2014-05-20 30 views
8

Xin chào, tôi nhận được mã thông báo truy cập google plus mà không cần sử dụng ID ứng dụng khách OAuth 2.0 có phạm vi. Nhưng với mã thông báo truy cập này không tìm nạp địa chỉ email. Cách tìm nạp địa chỉ email của người dùng?Android - cách nhận mã thông báo truy cập google plus?

Có sự khác biệt nào giữa được báo cáo có và không có ID ứng dụng OAuth 2.0 không?

Tôi đã sử dụng đoạn mã sau,

String accessToken=""; 
        try { 
         accessToken = GoogleAuthUtil.getToken(
           getApplicationContext(), 
           mPlusClient.getAccountName(), "oauth2:" 
             + Scopes.PLUS_LOGIN + " " 
             + Scopes.PLUS_PROFILE); 

         System.out.println("Access token==" + accessToken); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
+0

Hiển thị một số mã mà bạn đã thử. –

+0

Vui lòng kiểm tra câu hỏi cập nhật –

+0

Ngoại lệ bạn đang nhận được là gì? –

Trả lời

4
String accessToken = ""; 
try { 
    URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo"); 
    // get Access Token with Scopes.PLUS_PROFILE 
    String sAccessToken; 
    sAccessToken = GoogleAuthUtil.getToken(
    LoginCheckActivity.this, 
    mPlusClient.getAccountName() + "", 
    "oauth2:" 
     + Scopes.PLUS_PROFILE + " " 
     + "https://www.googleapis.com/auth/plus.login" + " " 
     + "https://www.googleapis.com/auth/plus.profile.emails.read"); 
} catch (UserRecoverableAuthException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace();     
    Intent recover = e.getIntent(); 
    startActivityForResult(recover, 125); 
    return ""; 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (GoogleAuthException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
13

Có 2 cách đơn giản để có được sử dụng Email từ Google plus,

1.Through Plus.AccountApi.getAccountName như dưới đây,

String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

2.Thông qua plus.profile.emails.read scope and REST end point như bên dưới,

Lấy GooglePlus AccessToken

Bạn cần phải vượt qua " https://www.googleapis.com/auth/plus.profile.emails.read" phạm vi này để có được những AccessToken từ GooglePlus như dưới đây,

accessToken = GoogleAuthUtil.getToken(
           getApplicationContext(), 
           mPlusClient.getAccountName(), "oauth2:" 
             + Scopes.PLUS_LOGIN + " " 
             + Scopes.PLUS_PROFILE+" https://www.googleapis.com/auth/plus.profile.emails.read"); 

Thực hiện cuộc gọi REST để các thiết bị đầu cuối và làm JSON đơn giản phân tích

https://www.googleapis.com/plus/v1/people/me?access_token=XXXXXXXXXXXXX

Bạn phải khai báo sự cho phép <uses-permission android:name="android.permission.GET_ACCOUNTS" /> trong số AndroidManifest.xml của bạn để sử dụng các phương pháp này.

Full Ví dụ từ trang web của nhà phát triển của Google,

làm cái gì đó như dưới đây để lấy Email của người dùng được chứng thực từ Google plus,

class UserInfo { 
    String id; 
    String email; 
    String verified_email; 
} 

final String account = Plus.AccountApi.getAccountName(mGoogleApiClient); 

AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() { 

    @Override 
    protected UserInfo doInBackground(Void... params) { 
    HttpURLConnection urlConnection = null; 

    try { 
     URL url = new URL("https://www.googleapis.com/plus/v1/people/me"); 
     String sAccessToken = GoogleAuthUtil.getToken(EmailTest.this, account, 
     "oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read"); 

     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestProperty("Authorization", "Bearer " + sAccessToken); 

     String content = CharStreams.toString(new InputStreamReader(urlConnection.getInputStream(), 
      Charsets.UTF_8)); 

     if (!TextUtils.isEmpty(content)) { 
     JSONArray emailArray = new JSONObject(content).getJSONArray("emails"); 

     for (int i = 0; i < emailArray.length; i++) { 
      JSONObject obj = (JSONObject)emailArray.get(i); 

      // Find and return the primary email associated with the account 
      if (obj.getString("type") == "account") { 
      return obj.getString("value"); 
      } 
     } 
     } 
    } catch (UserRecoverableAuthException userAuthEx) { 
     // Start the user recoverable action using the intent returned by 
     // getIntent() 
     startActivityForResult(userAuthEx.getIntent(), RC_SIGN_IN); 
     return; 
    } catch (Exception e) { 
     // Handle error 
     // e.printStackTrace(); // Uncomment if needed during debugging. 
    } finally { 
     if (urlConnection != null) { 
     urlConnection.disconnect(); 
     } 
    } 

    return null; 
    } 

    @Override 
    protected void onPostExecute(String info) { 
     // Store or use the user's email address 
    } 

}; 

task.execute(); 

Fore biết thêm đọc

https://developers.google.com/+/mobile/android/people

+0

Thanks.Got it. . –

+0

Tôi đã chỉnh sửa câu trả lời của mình với một số thông tin khác. Hy vọng nó sẽ giúp bạn. –

Các vấn đề liên quan