Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Invalid grant issue with Google OAuth authentication in Qt
Forum Updated to NodeBB v4.3 + New Features

Invalid grant issue with Google OAuth authentication in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
oauth2.0google api
1 Posts 1 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HRZZRH
    wrote on 14 May 2019, 05:43 last edited by
    #1

    I'm developing a Qt application and I want to use Google authentication for it. I created a Google API as explained in the following link: https://blog.qt.io/blog/2017/01/25/connecting-qt-application-google-services-using-oauth-2-0/ but I have a problem with it. It doesn't work in many cases and I get ProtocolInvalidOperationError(302) error for https://accounts.google.com/o/oauth2/token request URL in
    QOAuthHttpServerReplyHandler::networkReplyFinished(QNetworkReply *reply)
    method of Qt class.

    Note that I override QOAuthHttpServerReplyHandler::networkReplyFinished(QNetworkReply *reply) to get this error, because it doesn't emit any signal in this case, and the return value for reply->readAll() is as below:

    {
      "error": "invalid_grant",
      "error_description": "Malformed auth code."
    }
    

    My Login.cpp code is something as below:

    Login::Login() {
        google = new QOAuth2AuthorizationCodeFlow;
        google->setScope("email");
        google->setAuthorizationUrl("https://accounts.google.com/o/oauth2/auth");
        google->setClientIdentifier(Utility::decrypt(encryptedClientId));
        google->setAccessTokenUrl("https://accounts.google.com/o/oauth2/token");
        google->setClientIdentifierSharedKey(Utility::decrypt(encryptedClientSecret));
    
        connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
          &QDesktopServices::openUrl);
    
        connect(google,&QOAuth2AuthorizationCodeFlow::authorizationCallbackReceived,[=](const QVariantMap data){
    
        QString code(data["code"].toString());
        if(!code2.isEmpty())
        {
            const QUrl redirectUri= "http://localhost:56413/cb";
            QJsonObject postdata;
            postdata.insert("code",code);
            postdata.insert("client_id", Utility::decrypt(encryptedClientId));
            postdata.insert("client_secret", Utility::decrypt(encryptedClientSecret));
            postdata.insert("redirect_uri", redirectUri.toString());
            postdata.insert("grant_type","authorization_code");
    
           QString serviceURL = "oauth2/v4/token";
           NetworkManager::GetInstance()->Post(postdata,serviceURL,"https://www.googleapis.com/",[=](int statusCode,int resultnumber, QJsonObject obj){
    		   if (statusCode >= 200 &&   statusCode < 300) {
    			   // it's ok, do nothing
    		   }
    		   else {
                   //show error
                       }
               });  
            }
        });
    }
    
    void Login::googleLoginButtonPressed() {
        int googlePort = 56413;
        if(replyHandler == nullptr)
    	replyHandler = new QOAuthHttpServerReplyHandlerArio(googlePort, this);
        google->setReplyHandler(replyHandler);
    
        QObject::connect(replyHandler, &QOAuthHttpServerReplyHandler::tokensReceived, [=](const QVariantMap &map) {
    	googleToken = map["id_token"].toString();
    	connect(google, &QOAuth2AuthorizationCodeFlow::granted, [=]() {
    		auto reply = google->get(QUrl("https://www.googleapis.com/plus/v1/people/me"));
    		connect_reply = connect(reply, &QNetworkReply::finished, [=]() {
    			int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    			if (statusCode >= 200 &&
    				statusCode < 300)
    			{
    				//NOW register or login the user with email
    
    				QJsonDocument jsonResponse = QJsonDocument::fromJson(reply->readAll().data());
    				email = jsonResponse.object().value("emails").toArray()[0].toObject().value("value").toString();
    				reply->deleteLater();
    			}
    			else {
    				//error
    			}
    		});
    	});
        });
    
        google->grant(); 
    }
    

    Also my Qt version is 5.9.1.
    what's the problem?

    Thanks for your help.

    1 Reply Last reply
    0

    1/1

    14 May 2019, 05:43

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved