简体   繁体   English

从Google,Google +登录Android访问令牌验证

[英]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*. 我想从Google获取访问令牌,然后将其作为JSON对象*发送到我的服务器。

On server I want to validate the access token and then store essential user information. 在服务器上,我想验证访问令牌,然后存储基本用户信息。

My server is written in Node.js. 我的服务器是用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. 您需要将身份验证令牌传递到https://www.googleapis.com/oauth2/v1/userinfo?access_token=网址。 It will return the JSON data for user information. 它将返回JSON数据以获取用户信息。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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