https://developers.google.com/accounts/docs/OAuth2InstalledApp
我通过以下链接让用户在 webview 中使用 Google 帐户注册
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 和根据 Google API 控制台给出的重定向 URI,即选择重定向 URI https://developers.google.com/accounts/docs/OAuth2InstalledApp
最后使用 view.getTitle() 获取浏览器标题栏中返回的授权码
之后需要发送另一个请求实际的请求可能如下所示:
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();
}
所以我收到了错误的令牌响应......我从昨天开始尝试这个,建议和帮助将不胜感激..我的主要目标是使用 android 中的 gmail 帐户获取用户信息