例外:401未经授权的错误| com.google.api.client.auth.oauth2.TokenResponseException:401未经授权

时间:2016-11-03 09:27:21

标签: java google-api youtube-api google-api-java-client service-accounts

我的代码: GoogleCredential凭证

    try {
        // Authorize the request.
        credential = new GoogleCredential.Builder()
                .setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(serviceAccId)
                .setServiceAccountPrivateKeyFromP12File(certFilePath)
                .setServiceAccountScopes(Lists.newArrayList(YouTubeScopes.YOUTUBE))
                .setServiceAccountUser(emailId)
                .build();

credential.refreshToken()

错误日志:

Error |
Exception: 401 Unauthorized
Error |
com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
Error |
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
Error |
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
Error |
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
Error |
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
Error |
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
Error |
    at com.google.api.client.auth.oauth2.Credential$refreshToken.call(Unknown Source)
Error |
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
Error |
    at viewlift.YouTubePublishService.authorizeCredentials(YouTubePublishService.groovy:82)
Error |
    at viewlift.YouTubePublishService$$FastClassBySpringCGLIB$$48c3206b.invoke(<generated>)
Error |
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
Error |
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
Error |
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
Error |
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
Error |
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
Error |
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
Error |
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
Error |
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
Error |
    at viewlift.YouTubePublishService$$EnhancerBySpringCGLIB$$6e11774.authorizeCredentials(<generated>)
Error |
    at viewlift.YouTubePublishService$authorizeCredentials.call(Unknown Source)
Error |
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
Error |
    at com.viewlift.ViewLiftUtilsService.createSocialAccount(ViewLiftUtilsService.groovy:128)
Error |
    at com.viewlift.ViewLiftUtilsService$$FastClassBySpringCGLIB$$53f6d4a5.invoke(<generated>)
Error |
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
Error |
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
Error |
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
Error |
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
Error |
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
Error |
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
Error |
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
Error |
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)

我创建服务帐户的步骤:

  1. 我使用凭据
  2. 在oauth 2.0中创建了一个Web应用程序
  3. 然后我拿了客户端ID并用此创建了一个服务帐户
  4. 现在我正在使用此服务帐户和从中生成的p12证书来验证并创建GoogleCredentials的对象
  5. 一旦刷新令牌,我就给了我401异常。
  6. 在这种情况下,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

YouTube API不支持服务帐户身份验证。

解决方法需要切换到Oauth2并对代码进行一次身份验证并保存刷新令牌以供日后使用。刷新令牌可能会过期但不是经常出现,但是如果它过期,您将不得不观看它并重新自动化您的代码。

  

YouTube Data API支持OAuth 2.0协议进行授权   访问私人用户数据。

  

服务帐户流支持服务器到服务器的交互    访问用户信息。但是,YouTube Data API会   支持这一流程。由于无法将服务帐户链接到   YouTube帐户