2014-05-20 6 views
8

हैलो मुझे स्कॉप्स के साथ OAuth 2.0 क्लाइंट आईडी का उपयोग किये बिना Google प्लस एक्सेस टोकन मिल रहा है। लेकिन इस एक्सेस टोकन के साथ ईमेल पता नहीं मिलता है। उपयोगकर्ता ईमेल पता कैसे लाया जाए?एंड्रॉइड - गूगल प्लस एक्सेस टोकन कैसे प्राप्त करें?

क्या ओएथ 2.0 क्लाइंट आईडी के साथ और बिना accesstoken के बीच कोई अंतर है?

मैं का इस्तेमाल किया है कोड के बाद,

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

कुछ कोड दिखाएं जिन्हें आपने आजमाया है। –

+0

कृपया अद्यतन प्रश्न –

+0

जांचें आप क्या अपवाद प्राप्त कर रहे हैं? –

उत्तर

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

वहाँ 2 सरल तरीके गूगल से उपयोगकर्ता ईमेल प्राप्त प्लस करने के लिए कर रहे हैं,

1.Through Plus.AccountApi.getAccountName नीचे की तरह,

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

2. plus.profile.emails.read scope and REST end point से नीचे,

GooglePlus AccessToken

आप " https://www.googleapis.com/auth/plus.profile.emails.read" इस दायरे के पारित करने के लिए नीचे दिए गए जैसे GooglePlus से AccessToken प्राप्त करने की आवश्यकता,

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

समाप्ति बिंदु को एक बाकी कॉल करें और सरल

पार्स करने JSON करते जाओ

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

आप घोषित करना चाहिए इन विधियों का उपयोग करने के लिए आपके AndroidManifest.xml में अनुमति <uses-permission android:name="android.permission.GET_ACCOUNTS" />

गूगल डेवलपर साइट से पूर्ण उदाहरण के लिए,

प्लस गूगल से प्रमाणीकृत उपयोगकर्ता का ईमेल पुनः प्राप्त करने के लिए नीचे दिए गए जैसे कुछ करो,

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(); 

फोर अधिक जानकारी इस

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

पढ़
+0

धन्यवाद। इसे समझो। । –

+0

मैंने कुछ और जानकारी के साथ अपना जवाब संपादित किया है। आशा है इससे आपकी मदद होगी। –

संबंधित मुद्दे