Adsense API获取'错误:需要登录'

时间:2016-12-01 21:21:08

标签: google-api google-oauth adsense-api

我正在使用API​​ Explorer工具为google adsense创建一些请求网址。以下是资源管理器工具生成的请求网址,该网址提供了今天收入的响应:https://www.googleapis.com/adsense/v1.4/reports?startDate=today&endDate=today&accountId=MY_ACCOUNT_ID&metric=EARNINGS&key=MY_API_KEY

但是,当我尝试在浏览器或我的代码中使用此URL时,会给出此响应:

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

我不确定我错过了什么使用这个网址。我在谷歌开发者控制台上激活了adsense apis。

2 个答案:

答案 0 :(得分:0)

access_token参数是用于访问公共数据的公共API密钥。

[['name'], 'required','message' => 'You must enter name'], 参数用于访问私人用户数据。

在您的情况下,您正在尝试访问您的私人帐户,并且应该使用访问令牌。

要获取访问令牌,您需要使用Oauth2或服务帐户对您的应用程序进行身份验证。获得访问令牌后,将其与您的请求一起发送:

  

https://www.googleapis.com/adsense/v1.4/reports?startDate=today&endDate=today&accountId=MY_ACCOUNT_ID&metric=EARNINGS&access_token=YourAccessToken

答案 1 :(得分:-2)

您需要使用API​​密钥替换MY_API_KEY,并以相同的方式替换MY_ACCOUNT_ID。