Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Google auth on mobile

Google auth on mobile

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 2 Posters 322 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.
  • S Offline
    S Offline
    SeVeT
    wrote on last edited by SeVeT
    #1

    Hello, I am trying to achieve working sign in with google.
    There is no problem as long as I use my app as desktop.
    Problem starts when trying to do it on android app.
    I have found two ways of doing this:

    1.Setting client OAuth identification in google dev console to web so i can get both client identifier and client identifier shared key so later i set them for QOAuth2AuthorizationCodeFlow.

    2.Setting client OAuth identification in google dev console to android app so i get only client identifier so i set it for QOAuth2AuthorizationCodeFlow and fill in my SHA1 key that i sign my app with when building.

    In first method, when the browser window opens, everyhing is working till google sends callback - since my app is in background QOAuthHttpServerReplyHandler cannot receive anything and browser waits for handler to receive callback before closing so effectively its stuck. If i close window manually and go back to my app everything is working fine but is there a way to automatically close that window without waiting for handler to receive callback?

    In second method I created key, fill it in google dev console and got only client identifier,
    so client identifier shared key stays empty. In project settings
    I have set Build Android APK step to sign my app with created key.
    After opening browser for sign in google respond with ERROR 400: invalid request

    Can anyone help me with either one or second solution?
    My code for auth is: quite easy and should provide me a token

    GoogleOAuth::GoogleOAuth(QObject *parent) : QObject(parent)
    {
    
        this->google = new QOAuth2AuthorizationCodeFlow(this);
         this->google->setScope("email");
         connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);
    
    
         const auto port = static_cast<quint16>(1234);
         this->google->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth"));
         this->google->setAccessTokenUrl(QUrl("https://oauth2.googleapis.com/token"));
         this->google->setClientIdentifier(Zm().googleClientId);
         this->google->setClientIdentifierSharedKey(Zm().googleClientPass);    
    
    
         this->google->setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) {
             // Percent-decode the "code" parameter so Google can match it
             if (stage == QAbstractOAuth::Stage::RequestingAccessToken) {
                 QByteArray code = parameters->value("code").toByteArray();
                 (*parameters)["code"] = QUrl::fromPercentEncoding(code);
             }
         });
    
         QOAuthHttpServerReplyHandler* replyHandler = new QOAuthHttpServerReplyHandler(port, this);
         this->google->setReplyHandler(replyHandler);
    
         connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, [=](){
             const QString token = this->google->token();
             qDebug() << " token : " << token;
             emit authorizeGoogle(token);
         });
    }
    
    
    void GoogleOAuth::click()
    {
        this->google->grant();
    }
    

    Thanks in advance!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hrrrmph
      wrote on last edited by Hrrrmph
      #2

      I was having the same issue. The app won''t process the response until I manually activate it. Adding following line to AndroidManifest.xml fixes it.

      <meta-data android:name="android.app.background_running" android:value="true"/>
      

      However there is another problem on how to redirect user back to App and I have not solved it.
      QOAuthUriSchemeReplyHandler with custom URI does not work as well. These methods are deprecated in favor of AppAuth. https://developers.google.com/identity/protocols/oauth2/native-app#redirect-uri_custom-scheme

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hrrrmph
        wrote on last edited by
        #3

        Ok solved.
        To enable AppLinks (in format https://your-website.com/yourcallback) besides setting up intent filters in AndroidManifest.xml you also need to configure them on google play console and deploy a json file to your website. https://developer.android.com/training/app-links#add-app-links

        1 Reply Last reply
        0

        • Login

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