Google에서 액세스 토큰을 가져 와서 내 서버에 JSON 개체 *로 보내고 싶습니다. 서버에서Google, Google+ 로그인에서 액세스 토큰 확인 Android
나는 액세스 토큰의 유효성을 검사하고 필수 사용자 정보를 저장합니다.
제 서버는 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();
}
같이해야한다. 정답을 표시하십시오. – abraham