2017-05-29 126 views
0

我想连接到谷歌联系人api来访问我的联系人/保存在谷歌的人,但它会抛出TokenResponseException:401未授权。我对Google Oauth2.0有点新颖。我已经根据需要将服务帐户密钥文件下载到我的项目根目录。TokenResponseException:401未授权当试图访问谷歌联系人Api

下面是代码:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.gdata.client.contacts.ContactsService; 
import java.io.IOException; 
import java.security.GeneralSecurityException; 
import java.util.Arrays; 

public class Connector { 
private static ContactsService contactService = null; 
    private static HttpTransport httpTransport; 

    private static final String APPLICATION_NAME = "example"; 
    private static final String SERVICE_ACCOUNT_EMAIL = "[email protected]"; 
    private static final java.util.List<String> SCOPE = Arrays.asList("https://www.google.com/m8/feeds/"); 

    private Connector() { 
     // explicit private no-args constructor 
    } 

    public static void main(String[] args) { 
     System.out.println(getInstance()); 
    } 

    public static ContactsService getInstance() { 
     if (contactService == null) { 
      try { 
       contactService = connect(); 
      } catch (GeneralSecurityException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return contactService; 
    } 

    private static ContactsService connect() throws GeneralSecurityException, IOException { 
     httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 

     java.io.File p12File = new java.io.File("example-e8135faedf4e.p12"); 

     // @formatter:off 
     GoogleCredential credential = new GoogleCredential.Builder() 
       .setTransport(httpTransport) 
       .setJsonFactory(JacksonFactory.getDefaultInstance()) 
       .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) 
       .setServiceAccountScopes(SCOPE) 
       .setServiceAccountPrivateKeyFromP12File(p12File) 
       .setServiceAccountUser("[email protected]") 
       .build(); 
     // @formatter:on 

     if (!credential.refreshToken()) { 
      throw new RuntimeException("Failed OAuth to refresh the token"); 
     } 

     ContactsService myService = new ContactsService(APPLICATION_NAME); 
     myService.setOAuth2Credentials(credential); 

     return myService; 
    } 
} 

但是以下异常被抛出:

com.google.api.client.auth.oauth2.TokenResponseException:401未经授权

com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105) at com.google.api.client.auth.oauth2.TokenRequest.executeU nparsed(TokenRequest.java:287) at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) at com.google.api.client.googleapis.auth.oauth2。 GoogleCredential.executeRefreshToken(GoogleCredential.java:384) 在 com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) 在Connector.connect(Connector.java:58)在 连接器.getInstance(Connector.java:31)在 Connector.main(Connector.java:25)BUILD SUCCESSFUL(总时间:9秒 )

回答

0

从这个基于,TokenResponseException可能由许多事情引起,如无效的客户端ID,客户端密钥或范围以及刷新令牌过度使用。它还声明here另一个可能的来源为“401未授权”例外是离开credential.refreshToken()。该调用是必要的,以将访问代码写入参考。

可能还有帮助的其他参考:401 response when athenticating via Service Account and OAuth2 to GoogleDrive

+1

我看到这么晚了,但感谢您的帮助。我能够解决这个问题,让我马上发布最终代码 –

+0

@MichaelGatumaSelvatico:你的解决方案是什么。 –