java - How to use token obtained using GoogleAuthUtil class to get user information? -
i building android app in want authenticate user using google account. using googleauthutil class obtain token google shown below
protected string fetchtoken() throws ioexception{ try { return googleauthutil.gettoken(act, email, scope); } catch (googleauthexception e) { e.printstacktrace(); } return null; }
here, act current activity, email value obtained using accountpicker , scope = audience:server:client_id:x x clien id of web application.
i getting long result eyjhbgcioijsuzi1niisimtpzci6imfhmtkwmjzlytgwnjyxnji4zjdiyzm5otgyndczztflyte0ntvhnwqifq.eyjpc3mioijhy2nvd....... id token don't know how use id token retrieve user infromation it.
please me in knowing how user information id token.
after bit of research, got following.
id token used verify authenticity of user , access token used know information of e-mail id through user login. access token obtained in same way id token shown below.
oauthscopes = "oauth2:"+"https://www.googleapis.com/auth/userinfo.email"+" "+"https://www.googleapis.com/auth/plus.login"; protected string fetchaccesstoken() throws ioexception{ try { return googleauthutil.gettoken(act, email, oauthscopes); } catch(googleplayservicesavailabilityexception e){ act.handleexception(e); } catch(userrecoverableauthexception e){ act.handleexception(e); } catch (googleauthexception e) { e.printstacktrace(); } return null; }
notice calue of oatuthscopes. can specify number of scopes space required application request permission user.
now, information of user these scopes, following.
url url = new url("https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="+accesstoken); stringbuffer val = returnjson(url); url = new url("https://www.googleapis.com/oauth2/v1/tokeninfo?alt=json&id_token="+idtoken); stringbuffer valverify = returnjson(url);
method returnjson(url) is
stringbuffer returnjson(url url) throws ioexception { httpurlconnection conn = (httpurlconnection) url.openconnection(); inputstream istream = conn.getinputstream(); bufferedreader read = new bufferedreader(new inputstreamreader(istream)); string line; stringbuffer val = new stringbuffer(); while((line = read.readline())!=null){ val.append(line); } return val; }
access token return like,
{ "id": "10271045940xxxxxx", "email": "xxxx", "verified_email": true, "name": "xxxx", "given_name": "xxxx", "family_name": "xxxx", "link": "https://plus.google.com/+xxxx", "picture": "https://lh4.googleusercontent.com/xxxx", "gender": "male", "locale": "xx"}
id token return similar things. can print in logcat preview.
and then,
jsonobject reader = new jsonobject(val.tostring()); string email = (string)reader.get("email")); string name = (string)reader.get("name")); reader = new jsonobject(valverify.tostring()); boolean verified = reader.getboolean("email_verified"));
Comments
Post a Comment