Authenticating with Google

Quickly share your Android Code Snippets here...

Authenticating with Google

Postby jonnale » Thu Apr 01, 2010 11:17 pm

I was asked by ruhalmi if it would be possible to post a code snippet of how I authenticated with Google Docs on Android for accessing spreadsheet data. Here is my snippet:

Note before I begin: The GData API does not work with Android. I am not sure what package you are using, but I used this one: http://code.google.com/p/android-gdocs- ... e/checkout

If there is another package you can use for GDocs stuff, please let me know. I would love to check it out!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // first, we will declare some variables
  2. // I will initialize on separate line, just so you can see what variables you need
  3.  
  4. private SpreadsheetsClient sc;
  5. private ServiceDataClient sdc;
  6. private GDataParserFactory dspf;
  7.  
  8. // now we will initalize these variables
  9. sdc = new ServiceDataClient("writely", "https", "https://docs.google.com");
  10. //sdc.setSSL(true); //set this if you want to use SSL
  11. dspf = new XmlSpreadsheetsGDataParserFactory(new AbstructParserFactory());
  12.  
  13. sc = new SpreadsheetsClient(sdc, dspf);
  14.  
  15. // now we will try to login
  16. try {
  17.         sc.setUserCredentials(<put your username here>, <put your password here>);
  18. } catch (AuthenticationException e) {
  19.         // TODO Auto-generated catch block
  20.         Log.d("Authentication ERROR", "FAILED");
  21. }
  22.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


After this is done, go into your AndroidManifest.xml and add the following privileges:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.INTERNET"/>
  2. <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
  3.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Just to clarify, I am not sure if BOTH of these are needed. I wrote this code a few weeks ago, and I can't remember why I have both. You definitely need the INTERNET permission, I am not sure about the WRITE_SETTINGS permission.

And that should be it!

Let me know if there are any problems with this code. I copied and pasted most of it directly from my project, which works.

Hope that helps!
jonnale
Junior Developer
Junior Developer
 
Posts: 22
Joined: Wed Dec 30, 2009 11:02 pm

Top

Postby ruhalmi » Thu Apr 01, 2010 11:43 pm

Oh, great.

It works perfectly. For accessing spreadsheets you don't need the WRITE_SETTINGS permission, perhaps you've used it somewhere else.

Thanks again for sharing,
Rudo
ruhalmi
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Mon Nov 09, 2009 8:58 am

Postby jonnale » Thu Apr 01, 2010 11:45 pm

ruhalmi wrote:Oh, great.

It works perfectly. For accessing spreadsheets you don't need the WRITE_SETTINGS permission, perhaps you've used it somewhere else.

Thanks again for sharing,
Rudo


Awesome!

Glad I could help! :)
jonnale
Junior Developer
Junior Developer
 
Posts: 22
Joined: Wed Dec 30, 2009 11:02 pm

Postby emooney » Fri Apr 02, 2010 8:49 pm

Thanks jonnale,

What object are you pulling the username and password values from? Or are you asking then to provide them to you? If you're asking them, do they just have to provide it once?

Thanks
emooney
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jan 26, 2010 11:03 pm

Postby jonnale » Fri Apr 02, 2010 8:55 pm

emooney wrote:Thanks jonnale,

What object are you pulling the username and password values from? Or are you asking then to provide them to you? If you're asking them, do they just have to provide it once?

Thanks


You can store it if you want. I allow users to add spreadsheets from different accounts, so I store their username and password in sqlite AFTER I encrypt them to ensure privacy and protection. There are other ways to do this, but I was already using sqlite for other data, so implementing it this way took me like 5 minutes of programming and was very convenient.
jonnale
Junior Developer
Junior Developer
 
Posts: 22
Joined: Wed Dec 30, 2009 11:02 pm

Re: Authenticating with Google

Postby BorkFrey » Thu Nov 10, 2011 9:25 am

general question: Why would you need to authenticate to read data on Google forums? Isn't it all open to public? It's possible to get any info, even if it's really hidden. As long as your own cell phone can be linked to the internet you can track a cell phone , each and every sms, phone call, or possibly a geographic area modification
BorkFrey
Freshman
Freshman
 
Posts: 2
Joined: Tue Nov 08, 2011 9:31 am

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 5 guests