2014-10-21 2 views
0

Google 플러스와 사용자가 Google plus를 사용하여 로그인하고 Parse에 자격 증명이 저장되는 Parse와 통합하려고합니다. Google 플러스에서 사용자 토큰 받기하기

이를 위해

, 나는 다음과 같은 코드를 추가 한 :

이제
private void onGoogleButtonClicked() { 
     ParseUser.becomeInBackground("session-token-here", new LogInCallback() { 
        public void done(ParseUser user, ParseException e) { 
         if (user != null) { 
          // The current user is now set to user. 
         } else { 
          // The token could not be validated. 
         } 
         } 
        }); 

      } 
     }); 

내 문제는 구글 플러스에서 파생 된 세션 토큰을 찾는 남아있다. 다음 코드로 여행을 시작했습니다.

final String token = GoogleAuthUtil.getToken(context, 
         mPlusClient.getAccountName(), scope); 

이제 문맥, mPlusCLient 및 범위로 작업해야합니다. 내가이 문제로 당분간 고심하고있는 한 어떤 도움이라도 인정 될 것입니다.

답변

0

토큰을 가져 오는 데 Asynctask을 입력해야이 프로세스에서 UI를 더듬지 않게 할 수 있습니다. 로그인 한 사용자의 account name을 가져오고 Asynctask으로 전화하여 토큰을 받으십시오.

private class RetrieveTokenTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String accountName = params[0]; 
     String scopes = "oauth2:profile email"; 
     String token = null; 
     try { 
      token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes); 
     } catch (IOException e) { 
      Log.e(TAG, e.getMessage()); 
     } catch (UserRecoverableAuthException e) { 
      startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED); 
     } catch (GoogleAuthException e) { 
      Log.e(TAG, e.getMessage()); 
     } 
     return token; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     Toast.makeText(getApplicationContext(), "Token : " + s, Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

당신은 다음과 같은 방식으로 Asynctask를 호출 할 수 있습니다 :

String mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient); 
new RetrieveTokenTask().execute(mAccountName); 
다음은 샘플 코드는
관련 문제