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 
    } 
}); 
相關問題