2014-06-06 3 views
0

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

같이해야한다. 정답을 표시하십시오. – abraham

답변

2

당신은 https://www.googleapis.com/oauth2/v1/userinfo?access_token= URL로 인증 토큰을 전달해야합니다. 사용자 정보를위한 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 
+0

Ohk도 토큰의 유효성을 검사하고 프로필 내용을 반환합니까? 그것은 서버에 다른 것을 쓰지 않아도된다는 뜻입니다. 위의 URL로 토큰을 전달하면 정보를 확인하고 반환합니다. 내가 틀렸다면 나를 바로 잡아라. –

+0

예. 액세스 토큰이 유효하지 않으면 액세스 토큰이 유효하지 않음을 나타내는 메시지와 함께 JSON을 반환합니다. 모든 익스플로러에서 url과 함께 잘못된 토큰을 전달하여이를 확인할 수 있습니다. –

+0

내 액세스 토큰이 "k123abc"라고 말해 보겠습니다. 그래서 나는이 길을 통과해야합니까? https://www.googleapis.com/oauth2/v1/userinfo?k123abc= 또는 "https://www.googleapis.com/oauth2/v1/userinfo?access_token="+ k123abc ?? 명확히하십시오! –

관련 문제