我是安卓新手。我尝试使用 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();
我可以知道它为什么会发生以及如何解决它吗?
我是否正确使用了 google http/oauth2 api?
非常感谢。
此致,