1
votes

My Code: GoogleCredential credential

    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 Log:

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)

Steps i followed to create service account:

  1. I created a web application in oauth 2.0 in credentials
  2. Then i took the client id and with this i created a service account
  3. Now i am using this service account and p12 certificate generated from it to authenticate and create object of GoogleCredentials
  4. As soon as it goes for refresh token i given me 401 exception.

Any help would be appreciated in this case

1

1 Answers

1
votes

The YouTube API does not support Service account authentication.

Work around you will need to switch to Oauth2 and authenticate you code once and save the refresh token for use later. Refresh tokens can expire however its not that often but you will have to watch it and reauthentcate your code if it does expire.

The YouTube Data API supports the OAuth 2.0 protocol for authorizing access to private user data.

and

The service account flow supports server-to-server interactions that do not access user information. However, the YouTube Data API does not support this flow. Since there is no way to link a Service Account to a YouTube account