简体   繁体   中英

Access token validation from Google, Google+ Sign-in Android

I want to fetch an access-token from Google and then send it to my server as a JSON object*.

On server I want to validate the access token and then store essential user information.

My server is written in Node.js. How to do that?

 @Override
public void onConnected(Bundle connectionHint) {

    Log.v(TAG, "Connected. Yay!");

    findViewById(R.id.sign_in_button).setVisibility(View.INVISIBLE);


    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String code;
            Bundle appActivities = new Bundle();
            appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
                    "http://schemas.google.com/AddActivity");
            String scopes = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;

            try {
                 code = GoogleAuthUtil.getToken(
                        AuthenticationActivity.this,         // Context context
                        mPlusClient.getAccountName(),     // String accountName
                        scopes,                           // String scope
                        appActivities                     // Bundle bundle
                );

            } catch (UserRecoverableAuthException e) {
                // Recover
                code = null;
                //System.out.println(e.printStackTrace());
                AuthenticationActivity.this.startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);

            } catch (Exception e) {
                throw new RuntimeException();

            }
            return code;
       }

        @Override
        protected void onPostExecute(String token) {

           /* if(token!=null)
            {
             Log.i(TAG, "Access token retrieved:" + token);
             //SharedPreference = getApplicationContext().getSharedPreferences("TokenPreference", 0); 
             //editor = SharedPreference.edit();
             editor.putString("access_token",token);                               
             editor.commit();                      

            } */

            try
            {
               HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://gumbox1.cloudapp.net:3000");
                JSONObject data = new JSONObject();
                data.put("data", token);
               HttpEntity entity = new StringEntity(data.toString());
               BufferedReader reader = new BufferedReader(new InputStreamReader(client.execute(post).getEntity().getContent()));
               String response = reader.readLine();
               Log.e("response", response);
            }
            catch(Exception e)
            { Log.e("",e.toString());
            }





        }



      };

    task.execute();
 }

You need to pass auth token to https://www.googleapis.com/oauth2/v1/userinfo?access_token= url. It will return the JSON data for user information.


You should do like

private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=";

URL url = new URL(USER_INFO_URL + code);
con = (HttpURLConnection) url.openConnection();
InputStream is = con.getInputStream();
// Now convert into String and then into Json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM