PDA

View Full Version : Qt app using oAuth2.0 for authentification



SteveQt
24th November 2015, 15:48
Hi,

I have to create an application in which the user will provide a username and a password to connect to a web server and I have to do it with oAuth2.0.
So I created the following (by looking at Facebook and Twitter demo provided with oAuth).




const char CLIENT_ID[] = "...";
const char CLIENT_SECRET[] = "...";
const char REQUEST_URL[] = "...";
const char TOKEN_URL[] = "...";

oAuthClass::oAuthClass(QObject *parent) :
QObject(parent)
{
O2* o2Auth = new O2(this);

o2Auth->setClientId(CLIENT_ID);
o2Auth->setClientSecret(CLIENT_SECRET);
o2Auth->setRequestUrl(REQUEST_URL);
o2Auth->setTokenUrl(TOKEN_URL);

// Create a store object for writing the received tokens
O2SettingsStore *store = new O2SettingsStore(O2_ENCRYPTION_KEY);
store->setGroupKey("oAuth");
o2Auth->setStore(store);
}



And I am completely stuck there :(, I don't know what to do :confused: .
I think that I have to use the O2Requestor class which permits to create post request. Something like :




QNetworkAccessManager *manager;
O2Requestor* o2Requestor = new O2Requestor(manager,o2Auth);

QNetworkRequest* request = new QNetworkRequest(...);
/* I have the informations for the header
{ { "Authorization", "Basic <CLIENT_ID:CLIENT_SECRET>" },
{ "ContentType", "x-www-form-urlencoded" },
{ "grant_type", "password" },
{ "username", "<username>" },
{ "password", "<userpassword>" } }

so I can create a post request.
*/

o2Requestor->post(request, ...);


So are those steps good or am I wrond? :)
In addition, there is a lot of signals / slots in O2 class. I think I have to use them if I want to know if the connection is ok, if the token must be refreshed, ...
(If you knwon a good tutorial about how to use oAuth2.0 in Qt C++, I would appreciate a lot :) )
Thanks.

jefftee
25th November 2015, 21:48
I haven't used this personally, but found this very quickly via google search for "qt oauth2":

https://github.com/cybercatalyst/qtoauth2

Might be worth looking at.

pablodiazgutierrez
19th December 2020, 01:48
Things have changed quite a bit since you first asked. Now there's QNetworkAuthorization that makes it easier to authenticate Qt with Google SSO in C++ (https://appfluence.com/productivity/how-to-authenticate-qt-google-sso/). There are still a lot of caveats specific to Google, which is interesting because they announced they're about to block SSO from embedded web views, and everyone is scrambling to find a fix.