Android Question Problems with oAuth

mw71

Active Member
Licensed User
hi,

i have trouble with oAuth for Google Drive (because it is no longer possible to use WebView for oAuth)

I have
- import this Class: https://www.b4x.com/android/forum/threads/class-b4x-google-oauth2.79426/
- add Manifest
- generate OAuth Client ID
- use Code:
B4X:
Dim oAuth As GoogleOAuth2
Dim scope As String
Dim clientID As String

scope = "https://www.googleapis.com/auth/drive"
clientID = "xxx-xxxxxxxl.apps.googleusercontent.com"

oAuth.Initialize(Me,"oauth",clientID,scope)

wait for Init finisch...

B4X:
oAuth.GetAccessToken

the oAuth Page is Display, User allowed Access, no Access Token will display? Browser open the Google Page.
The CallFromResume will not be called (in Debug mode)??

please Help :)
 

DonManfred

Expert
Licensed User
Longtime User
Maybe you need to use another Scope?

Drive API, v3
Scopes
https://www.googleapis.com/auth/drive.appdata View View'and manage its own configuration data in your Google Drive
https://www.googleapis.com/auth/drive.fileView' and manage Google Drive files and folders that you have opened or created with this app
https://www.googleapis.com/auth/drive.metadata View'and manage metadata of files in your Google Drive
https://www.googleapis.com/auth/drive.metadata.readonly View'metadata for files in your Google Drive
https://www.googleapis.com/auth/drive.photos.readonly View'the photos, videos and albums in your Google Photos
https://www.googleapis.com/auth/drive.readonly View'the files in your Google Drive
https://www.googleapis.com/auth/drive.scripts Modifyyour Google Apps Script scripts' behavior
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i think the Scope is o.k. (?):
I tried the examplle in the oauth tutorial... The one for the google contacts...

I rewrote the initialization to use drive scrope and my clientid....

After i accepted my account on the google page i got back to the app and there i ran into a error because the example is then requesting contacts. But with the Drive scope and this is the error i got. Wrong Scope for this request.

What would be the httpjob to request drive data? I can use it in my example here and try it.

I dont know as i dont use Drive. have a account but not using it.

I tried
B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download2("https://www.googleapis.com/drive/v3/files", _
         Array As String("access_token", Token, "corpora", "user"))
    Wait For (j) JobDone(j As HttpJob)
but it results in an error that i did not Added Drive api to my project. So i now added it.

The result now is

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
{
"kind": "drive#fileList",
"nextPageToken": "V1*3|0|CiMxaklPVnowWjRNb3QzbXQ1X0tURU9fTFM0Q3A3a3IzYUN5URIHEILK1aO0Kw",
"incompleteSearch": false,
"files": [
{
"kind": "drive#file",
"id": "0B1xvZz7_R8ihNHprd2k0aF9IajQ",
"name": "Sleep as Android Data",
"mimeType": "text/csv"
},
{
"kind": "drive#file",
"id": "1m2Tv7cA0a35TQNoWu0rdZ6qXVuo",
"name": "Online-Radroutenplaner für Deutschland",
"mimeType": "application/vnd.google-apps.map"
},
{
"kind": "drive#file",
"id": "1dnVPqB5uMDjgTS2zKCkO0vCG25c",
"name": "Unbenannte Karte",
"mimeType": "application/vnd.google-apps.map"
},
{
"kind": "drive#file",
"id": "0B1xvZz7_R8ihRXdXUE9PV19EX00",
"name": "Backup",
"mimeType": "application/vnd.google-apps.folder"
},
{
"kind": "drive#file",
"id": "0B1xvZz7_R8ihbkd2SUxEOU83YTQ",
"name": "PetesEgg.mp4",
"mimeType": "video/mp4"
},
{
"kind": "drive#file",
"id": "0B1xvZz7_R8ihektSQ2c4YjZDSE0",
"name": "LICENSE.txt",
"mimeType": "text/plain"
},
{
"kind": "drive#file",
"id": "1GVjn4rj7-CKzf4k_N9hM86_qrFLv-MrSVg",
"name": "IMG-20160508-WA0018.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1vNWNm93MspRz05H7uL-6O_FZwxCfVKiNlA",
"name": "IMG-20160327-WA0003.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1yXv-RQoxS4CjfP3NByoqJv-dT-3OVZ-e3A",
"name": "IMG-20160223-WA0002.jpeg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "19f6aECZimxCKwxLtYquPWJfDRDkxxV5BMQ",
"name": "IMG-20160608-WA0008.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "18JKflEbFtf7oyLxUq1ER3JJbEnuio8LPyA",
"name": "IMG-20160604-WA0002.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1Qtr1Q2EU_YudXpKzP2_1GDEESBew4io_2w",
"name": "IMG-20160510-WA0001.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1CYOIq3cC369SHGMV0LLce03E7cML3QeWXA",
"name": "IMG-20160411-WA0004.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "10nzmoyq4IgPXznmgqU3Iqq8UiPz9Dm91bA",
"name": "IMG-20160508-WA0015.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "18AeX930ir4XCeV1nE5nBZoeyBvgnzfntgQ",
"name": "IMG-20160511-WA0017.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1upunCafUgWZse2o3hExV5wyiG5IMwKFRGA",
"name": "IMG-20160423-WA0004.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1EjZS9n7zMOGf4HDiutZsYNEJk50jcsGYgg",
"name": "IMG-20160519-WA0001.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "178OOaaQKqniVIc51cABPZF7dJGR56dLzsw",
"name": "IMG-20160510-WA0004.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "10x96_drg1tQc_1QeuEs5zZDEVhocaI3w0w",
"name": "IMG-20160611-WA0005.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1xzJyWRQdLaLKWeOZWjG-TE1ujUC9SwmcZA",
"name": "IMG-20160303-WA0002.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1xu3xkP6wFnU2Z-g8FuS328bjsFC6zhjFog",
"name": "IMG-20160416-WA0001.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1z_5UD2MJ85YgIMA9JSDEl0sNQIWQAUg0RQ",
"name": "IMG-20160314-WA0005.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1JW2A8LJKL9d_Nyuf7xsKumLb_CSMywCpbA",
"name": "IMG-20160608-WA0011.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "14stXOTy9cbr1G8tgJloG15BWCIyDEzvVuw",
"name": "IMG-20160424-WA0000.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1X3GB1LfsdvQuGKvrmDe5aC5rpVxKuZmAEQ",
"name": "IMG-20160511-WA0009.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1HeuARBg93rzJ-y_nltXXLX9VuTuRCzaP4A",
"name": "IMG-20160610-WA0001.jpg",
"mimeType": "image/jpeg"
},
{
"kind": "drive#file",
"id": "1eUPDMt5VBt41ZPrUcVyUY9zjs
Message longer than Log limit (4000). Message was truncated.

I guess for me it is working.

So the question is. What do you forgot or implemented not corectly.

Can you send me your test project (maybe private conversation) so i could test it here with your credentials... Or maybe i see an error in the implementation.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Addition/Nachtrag

Your app will not help really. I cant start it as i am using another signkey than you and you need to bound the clientID with the packagename (no problem) and the SHA1 signnature (i have another).
But i maybe can see the problem in the implementation...
 
Upvote 0

mw71

Active Member
Licensed User
after private conversation with DonManfred the error was found, i think.
The packagename must be in lower case, unfortunately i used lower and upper cases.
 
Upvote 0

mw71

Active Member
Licensed User
After the (most) problems were solved, I again the problem with de Packetnamen.

- package name in lowercase, oAuth2 works, update in PlayStore does not work (wrong package name)
- Packet name in upper and lower case (original, as in the store), oAuth2 does not work (see Post 1), update goes.

I think the problem is the URI Redirect Value

Has anyone an idea how oAuth2 works with the original Packetnamen (in case of case, for example the.MyApp)?
 
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
Hello DonManfred, we need your help:

We have an app with B4A-3.82 that works in connection with Google Drive. As of August 9, 2017 does not send / receive data due to ::
Err 400: Error: redirect_uri_mismatch (Google lock WebView).
We updated to B4a 7.01 and applied all the modifications that we believe correct but we can not connect to Google Drive :::

Attach the zip project to see if you can identify the problem. Failing that if you have a verified connection project with Google Drive would save us a lot of time and effort.
Thank you very much in advance ...

Inside Main you can see the documentation of the steps that we followed :::

B4X:
    ' Google ya no permite Acceso a Google Drive mediante WebView.
    '    Err 400: Error: redirect_uri_mismatch

    ' Debe hacerse mediante Oauth 2.0
    ' Seguimos procedimiento de Erel:    https://www.b4x.com/android/forum/threads/class-b4x-google-oauth2.79426/
    ' Empezamos descargando proyecto:   B4A_GoogleOAuth2.zip y GoogleOAuth2.bas.
    '  Segun especificacion Erel, importar el modulo GoogleOAuth2.bas que viene por separado.
    '  Ok-Funciona para "https://www.googleapis.com/auth/user.birthday.read"

    ' Modificaciones para Google Drive
    ' Modificamos proyecto con mw71 :    https://www.b4x.com/android/forum/threads/googledrive-via-api-v3.80775/
    ' Necesitamos HttpUtils2_Src.zip:    https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/
    ' Añadimos HttpJob y HttpUtils2Services como modulos dentro del proyecto
    ' Añadimos OkHttp(1.20) como libreria del proyecto
    ' Importamos GooggleDrive como modulo del proyecto.

        '''oAuth TestConnect
        '''*** Service (HttpUtils2Service) Create ***
        '''** Service (HttpUtils2Service) Start **
        '''{
        ''' "error": {
        '''  "errors": [
        '''   {
        '''    "domain": "global",
        '''    "reason": "authError",
        '''    "message": "Invalid Credentials",
        '''    "locationType": "header",
        '''    "location": "Authorization"
        '''   }
        '''  ],
        '''  "code": 401,
        '''  "message": "Invalid Credentials"
        ''' }
        '''}
        '''OAuth, Getting access token from refresh token in TestConnect
        '''{
        ''' "error": "invalid_request",
        ''' "error_description": "client_secret is missing."
        '''}
        '''OAuth TestConnect, Refresh Token Error
        '''oAuth TestConnect
        '''{
        ''' "error": {
        '''  "errors": [
        '''   {
        '''    "domain": "global",
        '''    "reason": "authError",
        '''    "message": "Invalid Credentials",
        '''    "locationType": "header",
        '''    "location": "Authorization"
        '''   }
        '''  ],
        '''  "code": 401,
        '''  "message": "Invalid Credentials"
        ''' }
        '''}
        '''OAuth, Getting access token from refresh token in TestConnect

    ' Comprobando Credenciales :: son de ....0@gmail.com
    ' Private ClientId As String = "8586....9abpc.apps.googleusercontent.com"  '....
    ' Private clientSecret As String    = "VMR1....mMnu"
    ' son de ....@gmail.com google drive Nombre Mi...... Tipo Otro

    ' Creada nueva Oauth Tipo App Otro para drivetest.b4a
    ' ID ::            8586....0t68.apps.googleusercontent.com
    ' Secreto ::    i1LR....vOOO

        '''GoogleDrive, Access Token request
        '''Getting access token from refresh token...
        '''*** Service (HttpUtils2Service) Create ***
        '''** Service (HttpUtils2Service) Start **
        '''{
        ''' "error": "invalid_request",
        ''' "error_description": "client_secret is missing."
        '''}

    ' Modifico codigo añadiendo secreto:::
        '    oauth2.Initialize(Me, "oauth2", ClientId, clientSecret, "https://www.googleapis.com/auth/drive")
        '    Public Sub Initialize (Target As Object, EventName As String, ClientId As String, clientSecret As String, Scope As String)

        '''GoogleDrive Initiaize
        '''GoogleDrive ConnectToDrive
        '''GoogleDrive, Access Token request
        '''Getting access token from refresh token...
        '''*** Service (HttpUtils2Service) Create ***
        '''{
        ''' "error": "unauthorized_client",
        ''' "error_description": "Unauthorized"
        '''}

    ' Compruebo configuraciones desde b4a 3.82:::
    ' API         key\ ..........keystore                        Ok
    ' SHA1        DA:AE:....                                    Ok
    ' Package    .................b4a                            Ok
    ' EnConsola    Mi.............
    ' ClientId     8586..9abpc.apps                            Ok
    ' Secret     VMR1..UimMnu                                Ok

        '''Err 400: Error: redirect_uri_mismatch
        '''java.lang.RuntimeException: Class instance was not initialized (googleoauth2)

    ' Pedir ayuda a Manfred en:
    '    [URL]https://www.b4x.com/android/forum/threads/problems-with-oauth.80174/[/URL]

Package Name (lower-case)::
upload_2017-8-15_13-56-58.png
 

Attachments

  • 01-GoogleContactsForHelp.zip
    256.4 KB · Views: 203
Last edited:
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
Thanks for your reply "mw71". We have also read and followed your notes and suggestions. The Package Name was already in lowercase. If you have a module to test the access to google drive we would be of a very great help !!!
 
Upvote 0

mw71

Active Member
Licensed User
please test:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        GDrive.Initialize(Me, "Drive", ClientId, clientSecret)
    End If
    Activity.LoadLayout("1")
End Sub

oauth2.Initialize.... is double, GDrive.Initialize do that.
Whith GDrive.ConnectToDrive you request the Token....

sri, unfortunately I cannot continue to test at the moment (must go to work)
 
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
Ok. Now the problema is::

B4X:
'''        ** Activity (main) Resume **
'''        Main, Activity_Resume
'''        GoogleDrive ConnectToDrive
'''        GoogleDrive, Access Token request
'''        GoolgeDrive, Run Test Connect
'''        oAuth TestConnect
'''        *** Service (HttpUtils2Service) Create ***
'''        ** Service (HttpUtils2Service) Start **
'''        {
'''         "error": {
'''          "errors": [
'''           {
'''            "domain": "global",
'''            "reason": "insufficientPermissions",
'''            "message": "Insufficient Permission"
'''           }
'''          ],
'''          "code": 403,
'''          "message": "Insufficient Permission"
'''         }
'''        }
'''        OAuth, Getting access token from refresh token in TestConnect
'''        {
'''         "error": "invalid_request",
'''         "error_description": "client_secret is missing."
'''        }
'''        OAuth TestConnect, Refresh Token Error
'''        Google Drive, Test Connect Error

And this error is the same that occurs when the api is tested from the console developers google (console.developers.google.com) when the request is executed in "Execute without OAuth" mode.
 

Attachments

  • 00-Oauth2.jpg
    00-Oauth2.jpg
    60.8 KB · Views: 224
  • 01-Oauth2.jpg
    01-Oauth2.jpg
    116 KB · Views: 276
  • 02-Oauth2.jpg
    02-Oauth2.jpg
    100 KB · Views: 196
  • 03-Oauth2.jpg
    03-Oauth2.jpg
    58.1 KB · Views: 208
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
When the request is executed from the console in "Autorize and execute" mode, it works correctly. And that means that the method you use, does not include the necessary permissions to execute the request in an authorized way "authorized execution".

Our question is::
Has it ever worked for you?
Do you have a working module?
What modification is necessary to add permissions and launch the request in "Authorized mode" ???

************** addscope("")**************
 

Attachments

  • 04-Oauth2.jpg
    04-Oauth2.jpg
    80.3 KB · Views: 255
  • 05-Oauth2.jpg
    05-Oauth2.jpg
    83.3 KB · Views: 233
Last edited:
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
With your module, we have the same problema ::
B4X:
oAuth TestConnect
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
{
"error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
}
}
Getting access token from refresh token...
{
"error": "invalid_request",
"error_description": "client_secret is missing."
}

And we see, you do not use client_secret.
We may not have successfully created the credentials to connect to the Google Drive API.
Can you help us to create a credential correctly ???
 

Attachments

  • 06-Oauth2.jpg
    06-Oauth2.jpg
    124.2 KB · Views: 216
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
Okay.
After different tests we have successfully connected to Google Drive and "ShowFileList" correctly with the same credential. We believe that the problem occurred because the device did not launch the Google authorization request. (Images are attached)

Our solution:
Before running the application you must connect the device with the google account that will serve the data from Google Drive.
After running the application, the authorization request for Google Drive is already launched correctly.

We can only say one thing "mw71", many & many & many thanks for your help and your time !!
 

Attachments

  • 08-Oauth2.jpg
    08-Oauth2.jpg
    273.2 KB · Views: 211
  • 07-Oauth2.jpg
    07-Oauth2.jpg
    158.5 KB · Views: 246
Upvote 0
Top