问题
I am trying to upload a video to YouTube account on behalf of 'user@gmail.com' by YouTube data API V3 and I receive 'access_denied' error. According to google documentation:
I opened a service account using a google administrator account 'admin@gmail.com' and completed the steps in : www.developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority.
I logged in as 'user@gmail.com' and gave permissions to 'admin@gmail.com' service account I created in step 1. (in www.console.developers.google.com/permissions/projectpermissions 'permissions' tab => 'add members') add member
This is my request (JAVA
) :
youtube = new YouTube.Builder(
GoogleAuthImpl.HTTP_TRANSPORT,
GoogleAuthImpl.JSON_FACTORY,
new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("my-service-account@test.com")
.setServiceAccountPrivateKeyFromP12File(new File("my-service-account.p12"))
.setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
.setServiceAccountUser("user@email.com")
.build()
)
.setApplicationName("My App")
.build();
And this is the response: com.google.api.client.auth.oauth2.TokenResponseException: 403 Forbidden
{
"error" : "access_denied",
"error_description" : "Requested client not authorized."
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(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)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
Is it possible to upload video on behalf of users? Am I doing something wrong?
Also, I found a link to YouTube data API - Oauth 2.0 flows that may indicate that this operation (of exporting to YouTube on behalf of another user via service account) is not supported: www.developers.google.com/youtube/v3/guides/authentication#OAuth2_Flows
Would appreciate a lot your response!
回答1:
The YouTube API doesn't support service accounts you need to use Oauth2.
来源:https://stackoverflow.com/questions/35916974/upload-video-to-youtube-with-a-service-account-on-behalf-of-a-user-by-server-sid