我正在尝试通过YouTube数据API V3代表"user@gmail.com"将视频上传到YouTube帐户,但收到"access_denied"错误。根据谷歌文档:
-
我使用 Google 管理员帐户"admin@gmail.com"开设了一个服务帐户,并完成了 www.developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority 中的步骤。
-
我以"user@gmail.com"身份登录,并向我在步骤 1 中创建的"admin@gmail.com"服务帐户授予了权限。(在 www.console.developers.google.com/permissions/projectpermissions"权限"选项卡中 =>"添加成员") 添加成员
这是我的要求(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();
这是回应: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)
是否可以代表用户上传视频?我做错了什么吗?
另外,我发现了一个指向YouTube数据API - Oauth 2.0流的链接,该链接可能表明不支持此操作(通过服务帐户代表其他用户导出到YouTube):www.developers.google.com/youtube/v3/guides/authentication#OAuth2_Flows
非常感谢您的回复!
YouTube API 不支持您需要使用 Oauth2 的服务帐号。