https://developers.google.com/accounts/docs/OAuth2InstalledApp
我給用戶提供谷歌帳戶上申請在網頁視圖與下面的鏈接註冊使用獲得授權碼
webview.loadUrl("https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state=%2F&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&client_id=706645665586.apps.googleusercontent.com");
其中包含我的客戶端ID和重定向URI,隨着谷歌Android的驗證2.0 HTTP POST請求每通過谷歌API控制檯給出即選擇一個重定向URI https://developers.google.com/accounts/docs/OAuth2InstalledApp
終於得到了在瀏覽器的標題欄使用view.getTitle()
0123返回的授權碼之後另一個請求需要發送 實際要求可能看起來像:
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/y_jtre05wvb6QSPo0Tkx5AbLfWB
client_id=706645665586.apps.googleusercontent.com
client_secret={client_secret}&
redirect_uri=urn:ietf:wg:oauth:2.0:oob
grant_type=authorization_code
所以現在同時使HTTP POST請求..
DefaultHttpClient httpcl = new DefaultHttpClient();
HttpPost httpp = new HttpPost("https://accounts.google.com/o/oauth2/auth");
List<NameValuePair> a = new ArrayList<NameValuePair>();
a.add(new BasicNameValuePair("code", "4/y_jtre05wvb6QSPo0Tkx5AbLfWB"));
a.add(new BasicNameValuePair("client_id", "706645665586.apps.googleusercontent.com"));
try {
StringEntity mEntity = new StringEntity("");
mEntity.setContentType(" application/x-www-form-urlencoded");
httpp.setEntity(mEntity);
httpp.setEntity(new UrlEncodedFormEntity(a));
HttpResponse response1 = httpcl.execute(httpp);
String response = EntityUtils.toString(response1.getEntity());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
所以我越來越壞令牌響應.. 。我從昨天開始嘗試這個,並建議和幫助,將不勝感激。我的主要目標是獲取用戶信息使用gmail帳戶在Android
只是爲了確保:您每次都會生成一個新的授權碼,而此處的硬編碼碼僅用於演示目的? –
是啊,我總是生成一個新鮮的......並在我的webview頁面完成後調用此方法.....即onPageFinished(WebView視圖,字符串url) –
而且你還添加'client_secret','redirect_uri' ,'grant_type'爲'a',對吧?然後我沒有看到你的代碼有什麼明顯的錯誤。您也許可以嘗試最近推出的[Google Play服務](http://android-developers.blogspot.de/2012/09/google-play-services-and-oauth-identity.html)來處理OAuth(也適用於更安全的方式)爲你。一旦獲得訪問令牌,您就可以使用Google的[OAuth2 API庫](https://code.google.com/p/google-api-java-client/wiki/APIs#oauth2)獲取「 Userinfo'對象。 –