Error: "message": "Login Required" when using Youtube Analytics API

I work with youtube api. when I hit this URL " https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date=2015-01-01&end-date=2016-01-31&metrics=likes% 2Cdislikes & key = {API Key} "

he gives 401

{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } } 

but I found in the explorer " https://developers.google.com/apis-explorer/ ?" It works great.
How to fulfill the first request?

+16
source share
2 answers

In your request, you send the key = {your key} for the access token that you must send access_token = {your access token oauth2}

Note. The key is used for public queries. access token is for authenticated requests.

+19
source

If someone else using JWT authentication in the Google API comes across this question (for example, when using service accounts), be sure to include auth: <your jwtClient> in your API call, for example:

First enter the token:

 // Configure JWT auth client var privatekey = require("./<secret>.json") var jwtClient = new google.auth.JWT( privatekey.client_email, null, privatekey.private_key, ['https://www.googleapis.com/auth/drive'] ); // Authenticate request jwtClient.authorize(function (err, tokens) { if (err) { return; } else { console.log("Google autorization complete"); } }); 

Then call the API (but don't forget the auth:jwtClient )

 drive.files.create({ auth: jwtClient, resource: {<fileMetadata>}, fields: 'id' }, function (err, file) { if (err) { // Handle error } else { // Success is much harder to handle } }); 
+4
source

All Articles