Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

OAuth 2.0 / Google web services tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-30-2012, 02:55 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default OAuth 2.0 / Google web services tutorial

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

It is recommended to go over Google documentation: Using OAuth 2.0 for Installed Applications - Authentication and Authorization for Google APIs - Google Code

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.



In order to run this example you will need to first register a new application and fill ClientId and ClientSecret variables. It takes about two minutes to register an application: 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.



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.
Attached Files
File Type: zip OAuth2.zip (9.8 KB, 122 views)
Reply With Quote
  #2 (permalink)  
Old 01-30-2012, 03:14 PM
Basic4ppc Expert
 
Join Date: Jun 2011
Location: Hertfordshire, UK
Posts: 1,126
Send a message via Yahoo to thedesolatesoul
Default

Nice tutorial
This will come handy with so many google services!
Reply With Quote
  #3 (permalink)  
Old 01-30-2012, 03:18 PM
Basic4ppc Expert
 
Join Date: Jan 2011
Location: Swindon UK
Posts: 566
Default

Thanks Erel, I've just been banging my head against the wall looking at the Facebook API, it did occur to me that using HTTP may be a simpler solution and you have provided it.

I'll have a look at Google first, then Facebook later.

Thanks

Steve
__________________
Living proof that a little knowledge can be a dangerous thing!
Reply With Quote
  #4 (permalink)  
Old 02-10-2012, 12:55 AM
Junior Member
 
Join Date: Jan 2012
Posts: 14
Default

I'm going crazy for a http post to Google Fusion tables !!!!!!!!
I modified the example gmailcontacts OAuth 2.0 / Google web services tutorial for accessing google fusion tables. I get the access_token but the on HttpUtils.PostByte i get in sub hc_ResponsError StatusCode = 400 & Reason = "parse error near access_token" !!!

Erel Help me I'm crazy, I tried in a thousand ways!

here the code:

Dim su As StringUtils
Dim bytes() As Byte
Dim sdata As String
Dim GoogleFusionPath As String
Dim query, auth, tail As String

GoogleFusionPath="https://www.google.com/fusiontables/api/query"
query="INSERT INTO 842604 (Barcode, GPS, Data) VALUES ('22222222', '10.1 11.1', '28/01/2012')"

auth="&access_token="& AccessToken
tail = "&jsonCallback=?"

sdata = "sql=" & su.EncodeUrl(query & auth & tail ,"UTF8")
bytes=sdata.GetBytes("UTF8")

HttpUtils.PostBytes("INSERT",GoogleFusionPath,byte s)
Reply With Quote
  #5 (permalink)  
Old 02-12-2012, 07:25 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I guess that this problem is now solved: http://www.basic4ppc.com/forum/basic...-tables-3.html

You should avoid posting duplicate questions.
Reply With Quote
  #6 (permalink)  
Old 03-29-2012, 05:29 PM
margret's Avatar
Basic4ppc Expert
 
Join Date: Jun 2011
Location: USA
Posts: 504
Question No Email or Website Data

Hello, I have an issue I hope you can help with. I am using the OAuth 2.0 Tutorial Code Erel wrote. In the code listed below, it does not return any Emails or Websites, it return every thing else, Notes, Phone #'s, etc. I have logged on to Google with a PC and created a half dozen contacts with all the fields filled in. Now I run this code with just the message box to display the field names and values passed to XML_EndElement and all works but the email and websites. It does return [email] as the Name of Field but the Text is always blank, even though they are populated in Google's contacts. All other fields return their respective data just as they should. Any Ideas?

Code:
Sub XML_EndElement (Uri As String, Name As String, Text As StringBuilder)
    
Msgbox(Text & CRLF & CRLF & Uri, Name)
End Sub

Last edited by margret : 03-29-2012 at 06:08 PM.
Reply With Quote
  #7 (permalink)  
Old 03-29-2012, 06:05 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should check the contacts API. I guess that not all of the data returns in this feed. You should probably send another request for each contact to get more information.
Reply With Quote
  #8 (permalink)  
Old 03-29-2012, 06:22 PM
margret's Avatar
Basic4ppc Expert
 
Join Date: Jun 2011
Location: USA
Posts: 504
Question

@Erel, I really don't understand this stuff, but looking through the Google information, I found this:
-----------------------------------------------------------------------
To retrieve all of a user's contacts, send an authorized GET request to the following URL:

https://www.google.com/m8/feeds/contacts/{userEmail}/full

In your sample you used:

https://www.google.com/m8/feeds/contacts/default/full

I changed the above line to the feed with the [userEmail] in it and I get the same response. This is from the docs of Google Contacts API-3. Any other info you might can provide a blind horse?
Reply With Quote
  #9 (permalink)  
Old 03-29-2012, 07:44 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should try to send another request for each contact with this api:
https://developers.google.com/google...single_contact
Reply With Quote
  #10 (permalink)  
Old 03-29-2012, 08:07 PM
Basic4ppc Expert
 
Join Date: Jun 2011
Location: Hertfordshire, UK
Posts: 1,126
Send a message via Yahoo to thedesolatesoul
Default

Hi margret,
Try as Erel says for a single contact using:
Code:
https://www.google.com/m8/feeds/contacts/{userEmail}/full/{contactId}
Get the contactid from the first request.
Or even better try:
Code:
https://www.google.com/m8/feeds/contacts/default/full?updated-min=2007-03-16T00:00:00
Are you sure it does not return only the email addresses?
Because there is a 'max-results' limit which means mostly the old contacts are returned and the new ones you added are not returned.
Also, I had some problem due to contact groups. I think the 'My Contacts' group is not returned unless forced.
The API is a bit temperamental, you might have to experiment.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android Dropbox / OAuth Tutorial Erel Basic4android Getting started & Tutorials 24 12-04-2011 07:56 AM
OAuth library Erel Additional libraries and official updates 7 08-23-2011 03:05 PM
Tutorial Request: OAuth sample ssg Basic4android Updates and Questions 4 07-21-2011 01:04 PM
OAuth - How to roll your own (easy). adrianus Basic4android Getting started & Tutorials 1 07-18-2011 08:08 PM
Consuming .Net web services / XML web services batchfile Basic4android Updates and Questions 2 06-17-2011 07:35 AM


All times are GMT. The time now is 10:06 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0