Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

authentication - Using Google account to log in to an Android Application

I’m developing an application on Android and I want to allow users to log in with their Google account. How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You might want to authenticate the user using one of the google account already configured in your device like some of the apps do, for that follow the below link -

"Authenticating to OAuth2 Services" - http://developer.android.com/training/id-auth/authenticate.html

Download Sample from Google - Android SDK Manager/Extras/Google Play Services

In simple steps it does

  1. Shows list of accounts in your mobile
  2. Generates access token from selected accounts
  3. Gets the Account name from access token by contacting google services(seperate call) to just tell that it has authenticated.

This is another link which is good in explaining the process - http://android-developers.blogspot.in/2013/01/verifying-back-end-calls-from-android.html

you can follow below steps for Login in your app

  1. you will send the generated access token to your Back-end server
  2. Back-end server checks that access token is valid or not by contacting google services by this url "https://www.googleapis.com/oauth2/v1/userinfo?access_token=ACCESS_TOKEN"
  3. Next Back-end server responds to app whether to make the user login or not.

Below is response format of above "userinfo" call

{
 "id": "ID",
 "name": "NAME",
 "given_name": "GiVEN NAME",
 "family_name": "FAMILY_NAME",
 "link": "https://plus.google.com/ID",
 "picture": "https://PHOTO.jpg",
 "gender": "GENDER",
 "locale": "LOCALE"
}

If you want Email id along with that response you have to modify

SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile";

to

SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";

in that sample


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...