-
Notifications
You must be signed in to change notification settings - Fork 1
/
dropboxauth.cpp
79 lines (53 loc) · 2.79 KB
/
dropboxauth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "dropboxauth.h"
Dropboxauth::Dropboxauth(QObject *parent) : QObject(parent),
m_networkAccessManager(new QNetworkAccessManager(this))
{
this->dropbox = new QOAuth2AuthorizationCodeFlow(this);
// Set Scope or Permissions required from Dropbox
// This parameter allows your user to authorize a subset of the scopes selected in the App Console.
// Multiple scopes are separated by a space. If this parameter is omitted,
// the authorization page will request all scopes selected on the Permissions tab.
// Uncomment the line below to set scope
// this->dropbox->setScope("");
connect(this->dropbox, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, [=](QUrl url) {
QUrlQuery query(url);
query.addQueryItem("force_reapprove", "true"); // Param required to get data everytime
query.addQueryItem("token_access_type", "offline"); // Needed for Refresh Token (as AccessToken expires shortly)
query.addQueryItem("response_type", "code");
url.setQuery(query);
QDesktopServices::openUrl(url);
});
// Here the parameters from Dropbox JSON are filled up
// Attached screenshot of Dropbox Console
this->dropbox->setAuthorizationUrl(QUrl("https://www.dropbox.com/oauth2/authorize"));
this->dropbox->setAccessTokenUrl(QUrl("https://api.dropboxapi.com/oauth2/token"));
this->dropbox->setClientIdentifier("APP_KEY");
this->dropbox->setClientIdentifierSharedKey("APP_SECRET");
// In my case, I have hardcoded 5476
// This is set in Redirect URI in Dropbox Developers Console of the app
auto replyHandler = new QOAuthHttpServerReplyHandler(5476, this);
this->dropbox->setReplyHandler(replyHandler);
// If access granted, pull up file list
connect(this->dropbox, &QOAuth2AuthorizationCodeFlow::granted, [=](){
qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!";
QJsonObject obj;
obj.insert("limit", 100);
QJsonDocument doc(obj);
QString strJson(doc.toJson(QJsonDocument::Compact));
QNetworkRequest m_networkRequest;
m_networkRequest.setUrl(QUrl("https://api.dropboxapi.com/2/file_requests/list_v2"));
m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
m_networkRequest.setRawHeader("Authorization", "Bearer " + this->dropbox->token().toUtf8());
auto reply = m_networkAccessManager->post(m_networkRequest, strJson.toUtf8());
connect(reply, &QNetworkReply::finished, [=](){
qDebug() << "REQUEST FINISHED. Error? " << (reply->error() != QNetworkReply::NoError);
qDebug() << reply->readAll();
});
});
}
// Call this function to prompt authentication
// and receive data from Dropbox
void Dropboxauth::click()
{
this->dropbox->grant();
}