6

我正在使用youtube api。当我打这个网址 “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密钥}错误:使用YouTube分析API时出现“message”:“需要登录”

它给了401

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

,但我在浏览器hited “https://developers.google.com/apis-explorer/?” 它工作正常。
如何使第一个请求生效?

+0

您应该解决您的问题,这是对YouTube分析API的请求。您需要使用Oauth2对该请求进行身份验证。 – DaImTo

+0

我已经有了来自Oauth的访问令牌的不同意思。现在我如何获得这些数据? –

+0

请尝试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&access_token={access来自oauth的令牌} – DaImTo

回答

9

在您的要求您发送键= {密钥}访问令牌,你要发送的access_token = {您的OAuth2访问令牌}

注:主要用于公共请求。访问令牌用于已认证的请求。

0

如果有人在这个问题上使用谷歌API绊倒JWT认证(例如,使用服务帐户时。)然后请务必在您的API调用auth: <your jwtClient>,如:

首先,获得令牌:

// 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"); 
    } 
}); 

然后,调用API (但不要忘了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 
    } 
}); 
相关问题