-
Notifications
You must be signed in to change notification settings - Fork 4
/
googleauth.cpp
53 lines (40 loc) · 2.04 KB
/
googleauth.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
#include "googleauth.h"
GoogleAuth::GoogleAuth(QObject *parent) : QObject(parent)
{
this->google = new QOAuth2AuthorizationCodeFlow(this);
// Set Scope or Permissions required from Google
// List can be obtained from https://developers.google.com/identity/protocols/oauth2/scopes
this->google->setScope("email https://www.googleapis.com/auth/drive.readonly");
connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, [=](QUrl url) {
QUrlQuery query(url);
query.addQueryItem("prompt", "consent"); // Param required to get data everytime
query.addQueryItem("access_type", "offline"); // Needed for Refresh Token (as AccessToken expires shortly)
url.setQuery(query);
QDesktopServices::openUrl(url);
});
// Here the parameters from Google JSON are filled up
// Attached screenshot of JSON file and Google Console
this->google->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth"));
this->google->setAccessTokenUrl(QUrl("https://oauth2.googleapis.com/token"));
this->google->setClientIdentifier("MY_CLIENT_ID");
this->google->setClientIdentifierSharedKey("MY_SECRET_TOKEN");
// In my case, I have hardcoded 5476
// This is set in Redirect URI in Google Developers Console of the app
// Same can be seen in the downloaded JSON file
auto replyHandler = new QOAuthHttpServerReplyHandler(5476, this);
this->google->setReplyHandler(replyHandler);
connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, [=]() {
qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!";
auto reply = this->google->get(QUrl("https://www.googleapis.com/drive/v3/files"));
connect(reply, &QNetworkReply::finished, [reply]() {
qDebug() << "REQUEST FINISHED. Error? " << (reply->error() != QNetworkReply::NoError);
qDebug() << reply->readAll();
});
});
}
// Call this function to prompt authentication
// and receive data from Google
void GoogleAuth::click()
{
this->google->grant();
}