Android Tutorial OAuth 2.0 / Google web services tutorial

This tutorial is no longer relevant. Google doesn't allow using WebView to access the server response. Updated tutorial: https://www.b4x.com/android/forum/threads/class-b4x-google-oauth2.79426/

OAuth is an authentication method which allows the user to grant your application access to private information in a controlled way.

In order to access private information you need to get an access token.

There are several steps required (the attached code takes care for most of the steps):
- Register your application with Google. You will get "client id" and "client secret" values.

SS-2012-01-30_17.36.26.png


In order to run this example you will need to first register a new application and fill ClientId and ClientSecret variables: https://code.google.com/apis/console


- In your application you should show a WebView that handles the authentication.
(the user default language is used in this dialog)
The scope parameter should be set based on the required service.

SS-2012-01-30_17.38.36.png


After the user approves the access the WebView is redirected to a different page. The authorization code appears in the page title. We extract it using WebViewExtender library. After extracting the code the WebView is removed.

- Another Http call is required to get the access token.

Once we have the access token we can send requests that access the user's private data.
In the attached example we retrieve the user Gmail contacts list.

OAuth 2.0 is a popular authentication method. It shouldn't be difficult to use this code with other web services such as Twitter, Facebook and others. It is much simpler than OAuth 1.0.
The following libraries are required: HTTP, JSON (to parse the access token request) and WebViewXtended (to get the authorization code from the WebView title).
The attached example also requires XmlSax for the contacts feed parsing.

You will need to register a new application with Google in order to run the example. Once registered you should fill CliendId and ClientSecret variables.
It takes two minutes to register a new application.
 

Attachments

  • GmailContacts.zip
    8.1 KB · Views: 894
Last edited:

adastra

Member
Licensed User
Longtime User
hi erel
i am having problem to get contact list.
showing error, sending you log

** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (act_gmail) Create, isFirst = true **
** Activity (act_gmail) Resume **
Starting Job: GetAccessToken
** Service (httputilsservice) Create **
** Service (httputilsservice) Start **
Error. Url=https://accounts.google.com/o/oauth2/token Message=Unauthorized
{
"error" : "invalid_client"
}
** Service (httputilsservice) Destroy **
 

Sergio Martinez

New Member
Licensed User
Hi , I tried to run the gmailcontacts example and I have the following error.
B4A version: 5.80
Parsing code. (0.01s)
Compiling code. (0.21s)
Compiling layouts code. (0.00s)
Generating R file. (0.04s)
Compiling debugger engine code. (0.80s)
Compiling generated Java code. Error
B4A line: 24
If init = False Then Process_Globals
javac 1.8.0_77
src\anywheresoftware\b4a\samples\gmailcontacts\httputilsservice.java:332: error: cannot find symbol
_process_globals();};
^
symbol: method _process_globals()
location: class httputilsservice

What should I do?
Thank you so much.
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    51.7 KB · Views: 292
Last edited:

tpakis

Active Member
Licensed User
Longtime User
If you want to use this example with b4j in the google developer console make a new project of type "other" not android
 

tpakis

Active Member
Licensed User
Longtime User
In the project with the contacts, there is a comment in the getaccesstoken section about storing the refresh token. Is there any example on how to use it?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

tpakis

Active Member
Licensed User
Longtime User
I did the switch, i used this tutorial as a base for a B4J app. The link is very useful i couldn't find that info (and i really searched google's documentation). It's needed because the access tokens are very short lived only 1hour(!!!!) and i wouldn't like the user to go through the whole process every time he uses the app.
 

dbalman

Member
Licensed User
Longtime User
I did the switch, i used this tutorial as a base for a B4J app. The link is very useful i couldn't find that info (and i really searched google's documentation). It's needed because the access tokens are very short lived only 1hour(!!!!) and i wouldn't like the user to go through the whole process every time he uses the app.

Same here. However, I am unable to retrieve the data from the custom fields of individual contacts. The name, gContact:userDefinedField, is returned, but the key and value are not. https://developers.google.com/google-apps/contacts/v3/reference#gcUserDefinedField

OAuth2.0 Playground returns the key and value, so perhaps it is an issue with the jXMLSax library? It appears that any field that is not tagged at both ends is ignored, e.g.,
<gd:fullName>Forrest Gump</gd:fullName> is displayed as expected while <gContact:birthday when="1993-12-25"/> is not.

B4X:
<gContact:userDefinedField key="Ticket#" value="6789GTX1B"/>
<gContact:userDefinedField key="Client ID" value="987654321"/>
<gContact:userDefinedField key="DR#" value="16-078912"/>
<gContact:userDefinedField key="Case#" value="12-23456789"/>
<gContact:userDefinedField key="SS#" value="123-45-6789"/>
<gContact:userDefinedField key="Docket#" value="987456321"/>
<gContact:userDefinedField key="File#" value="8675309"/>
<gContact:userDefinedField key="Court Name" value="Hershey Town Court"/>
<gContact:userDefinedField key="Violation " value="VTL 1180D"/>
<gContact:userDefinedField key="Judge Name" value="Judge J. Executioner"/>
In any case, can anyone think of a workaround that would allow me to get the key and its value without manually parsing the XML file?
 
Last edited:

Similar Threads

Top