我知道以前有人问过这个问题,并且有一个经过验证的答案,这里,但是这个问题对我来说不管用。我想回复我的频道的评论,从我的定制软件。首先,我在google中创建了一个服务帐户,并创建了一个密钥,并下载了一个包含该键的文件(参见下面代码中的file.json )。我通过使用代码获得了Oauth2.0令牌:
try {
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("file.json"))
.createScoped(Collections.singleton(YouTubeScopes.YOUTUBE_FORCE_SSL));
String token = credentials.refreshAccessToken().getTokenValue();
} catch (IOException e) {
e.printStackTrace();
}
file.json文件来自google帐户控制台。它包括私钥、client_id、客户端电子邮件和其他细节。这段代码一切正常,我成功地接收了访问令牌。但是,当我试图用所需的json向https://youtube.googleapis.com/youtube/v3/comments?part=snippet发送http请求时,这里解释说,我得到了以下错误:
{
"error": {
"code": 403,
"message": "The YouTube account used to authorize the API request must be merged with the user's Google account.",
"errors": [
{
"message": "The YouTube account used to authorize the API request must be merged with the user's Google account.",
"domain": "youtube.comment",
"reason": "ineligibleAccount",
"location": "Authorization",
"locationType": "header"
}
]
}
}
提前谢谢。
发布于 2021-04-22 02:13:50
我在google控制台中创建了一个服务帐户
YouTube API不支持服务帐户身份验证。创建桌面应用程序凭据并使用Oauth2登录。
安装的应用程序的代码应该如下所示。
/**
* Initializes an authorized YouTube service object.
*
* @return The YouTube service object.
* @throws IOException
* @throws GeneralSecurityException
*/
private static YouTube initializeYouTube() throws GeneralSecurityException, IOException {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
// Load client secrets.
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(HelloYouTube.class
.getResourceAsStream(CLIENT_SECRET_JSON_RESOURCE)));
// Set up authorization code flow for all authorization scopes.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
.Builder(httpTransport, JSON_FACTORY, clientSecrets,
YouTubeScopes.all()).setDataStoreFactory(dataStoreFactory)
.build();
// Authorize.
Credential credential = new AuthorizationCodeInstalledApp(flow,
new LocalServerReceiver()).authorize("user");
// Construct the YouTube service object.
return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
从评论中
我发现在用户出现一些弹出的情况下无法对youtube api进行身份验证,因为OAuth2.0的登录和您的示例都要求用户在某些弹出窗口中选择帐户进行身份验证。
不,在YouTube API中没有其他选项。
为了澄清我自己,我希望身份验证在幕后进行,用户可以通过认证预定义的凭据发送回复。
在您可以授权应用程序一次存储刷新令牌,然后使用刷新令牌访问API之前,我已经这样做了。
我想知道这是否可能。
至少在未经授权一次的情况下不会。
https://stackoverflow.com/questions/67210122
复制相似问题