use*_*498 5 android httprequest oauth-2.0 google-oauth
我是android的新手.我尝试使用OAuth2实现一个简单的客户端/服务器连接,过程是,
尝试使用ClientCredentials(client_id和client_secret)连接到OAuth2服务器并获取访问令牌.
使用访问令牌注册用户.
所以它涉及两轮连接.第一轮总是很好,第二轮http连接总是返回EOFException,这让我很困惑.相关代码如下(它包含在一个在新线程中运行的过程中).
NetHttpTransport transport = new NetHttpTransport();
JacksonFactory factory = new JacksonFactory();
//use http for testing only, will use https for deployed environment
GenericUrl url = new GenericUrl("http://192.168.x.x/oauth2/token");
//client_id & client_secret
BasicAuthentication auth = new BasicAuthentication("abc","abc");
ClientCredentialsTokenRequest token =
new ClientCredentialsTokenRequest(transport, factory, url)
.setClientAuthentication(auth);
TokenResponse response = token.execute();
//ok, i can get access token in response without problem
Credential credential =
new Credential(BearerToken.authorizationHeaderAccessMethod())
.setFromTokenResponse(response);
//note that I reuse the transport, but using a new transport2 doesn't help
HttpRequestFactory rf = trasport.createRequestFactory(credential);
GenericData data = new GenericData();
data.put("Username",phoneText.getText().toString());
data.put("Password", passwordText.getText().toString());
JsonHttpContent content = new JsonHttpContent(factory, data);
GenericUrl genericUrl=new GenericUrl("http://192.168.x.x/users");
HttpRequest request = rf.buildPostRequest(genericUrl, content);
//the following will always return EOF exception
HttpResponse response=request.execute();
Run Code Online (Sandbox Code Playgroud)
我可以知道它为什么会发生以及如何解决它?
我正确使用谷歌http/oauth2 api吗?
非常感谢你.
最好的祝福,
如果您使用 Android,如果可能的话,您应该使用Google Play Services 中的GoogleAuthUtil类,而不是尝试从 Android 应用程序内部进行 OAuth 舞蹈。手机上的任何帐户都经过预先身份验证,并且可以很容易地获得任何合理的访问令牌。
\n| 归档时间: |
|
| 查看次数: |
323 次 |
| 最近记录: |