I know this question was asked before and has a verified answer here but that soultion does not work for me. I want to reply to my channel's comments from my custom software. First, I created a service account in google console and created a key and downloaded a file containing that key (see file.json in below code). I get my Oauth2.0 token with using code:
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 file is from google service account console. It includes private key, client_id, client email and other details. Everything works fine with this code and I successfully receive access token. But when I try to send a http POST request to https://youtube.googleapis.com/youtube/v3/comments?part=snippet with required json body explained here I get the following error:
{
"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"
}
]
}
}
Thanks in advance.
I created a service account in google console
The YouTube API doesn not support service account authentication. Create either a desktop app credentials and login using Oauth2.
Code for an installed app should be something like this.
/**
* 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();
}
I see that there's no way to authenticate youtube api without some popup appearing to user because both the login with Oauth 2.0 and your example require user to choose account to authenticate in some popup.
No there is no other option with the YouTube API.
Just to clarify myself, I wanted the authentication to happen behind the scenes and user can send reply by authenticating pre-defined credentials.
I have done this before you can authorize your application once store the refresh token and then use the refresh token to access the API in the future.
I was wondering if that would be possible.
Not without at least consenting to authorization once.