来自Google,Google +登录Android的访问令牌验证

时间:2014-06-06 08:40:50

标签: android node.js validation google-plus access-token

我想从Google获取访问令牌,然后将其作为JSON对象发送到我的服务器*。

在服务器上,我想验证访问令牌,然后存储必要的用户信息。

我的服务器是用Node.js编写的。怎么做?

 @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();
 }

1 个答案:

答案 0 :(得分:2)

您需要将身份验证令牌传递给https://www.googleapis.com/oauth2/v1/userinfo?access_token=网址。它将返回用户信息的JSON数据。


你应该这样做

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