2012-02-02 61 views
3

我试图让使用的Java对象:Google+的OAuth无效头

https://www.googleapis.com/oauth2/v1/userinfo?alt=json

我通过以前获得我的访问令牌Google+用户的信息:

URL googleURL = new URL("https://www.googleapis.com/oauth2/v1/userinfo?alt=json"); 

      URLConnection googleConn = googleURL.openConnection(); 
      googleConn.setAllowUserInteraction(false); 
      googleConn.setDoOutput(true); 

      **googleConn.setRequestProperty("Authorization", "Bearer "+accessToken);** 

      BufferedReader rd = new BufferedReader(new InputStreamReader(googleConn.getInputStream())); 

      String result = ""; 
      while(rd.ready()) 
       result += rd.readLine(); 
      rd.close(); 

但它给我的HTTP错误代码401(无效标题)。

这是在头中传递访问令牌的正确方法吗?

googleConn.setRequestProperty("Authorization", "Bearer "+accessToken); 

回答

1

要访问Google+个人资料信息你真的想使用人获得API:https://www.googleapis.com/plus/v1/people/me你可能要检查出reference docsAPI Explorer用它玩。

此外,您的编码水平远低于您的需要。虽然您可以直接使用REST端点,但如果使用official client library,事情会变得更容易。如果你走这条路线,甚至有一个Google+ starter project for Java

如果你想继续低级别,你应该使用OAuth前缀代替Bearer传入访问令牌。您生成的HTTP请求标头将如下所示:

GET https://www.googleapis.com/plus/v1/people/102817283354809142195 

Authorization: OAuth ya29.BHES61Rxr4Cq-Elcfpvx_2oWC443fMOEQV9iS5-M7ZjJ6xk