开发者令牌在此项目中不被允许 – Google广告API

huangapple go评论71阅读模式
英文:

Developer token is not allowed with project - Google Ads API

问题

I'm using OAuth2 flow to get access to a Google Ads Account. My application runs in a flask server, so I can get the authorization code from the OAuth2 flow using the request variable in the flask context. First, I get the permission screen URL, that redirects to the "/google_ads/authorized" flask route.

auth_url = 'https://accounts.google.com/o/oauth2/auth'
client_id = os.environ['CLIENT_ID']
client_secret = os.environ['CLIENT_SECRET']
redirect_uri = os.environ['REDIRECT_URI']
scope = 'https://www.googleapis.com/auth/adwords'

In the redirected flask route, I'm making a request to exchange the authorization code for the access token and refresh token.

token_url = 'https://accounts.google.com/o/oauth2/token'
payload = {
 "code": request.args['code'],
 "client_id": client_id,
 "client_secret": client_secret,
 "redirect_uri": redirect_uri,
 "grant_type": "authorization_code"
}
req = requests.post(url=token_url, data=json.dumps(payload))
res = req.json()

credentials = {
 'access_token': res['access_token'], 
 'refresh_token': res['refresh_token'], 
 'client_id': client_id,
 'client_secret': client_secret, 
 'expiry': '2023-06-12T18:01:59', 
 'scopes': ['https://www.googleapis.com/auth/adwords']
}

Finally, I use the credentials dict to build a Credentials object, which is passed with the developer token as an argument to instantiate the GoogleAdsClient.

from google.oauth2.credentials import Credentials
from google.ads.googleads.client import GoogleAdsClient

developer_token = os.environ['DEVELOPER_TOKEN']
credentials = Credentials.from_authorized_user_info(creds)
client = GoogleAdsClient(credentials=credentials, developer_token=developer_token)

customer_service = client.get_service("CustomerService")
customer_service.list_accessible_customers()

When I try to make a request and list the available customers in the account using customer_service.list_accessible_customers(), I get the following error:

{
 "error": {
   "code": 403,
   "message": "The caller does not have permission",
   "status": "PERMISSION_DENIED",
   "details": [
     {
      "@type": "type.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure",
      "errors": [
	 {
	   "errorCode": {
	      "authorizationError": "DEVELOPER_TOKEN_PROHIBITED"
	   },
	   "message": "Developer token is not allowed with project '685646918301'."
         }
       ],
       "requestId": "hYMHcD_FbdI0vwYeKRrAbw"
      }
    ]
  }
}

This method is the same method that I'm using to authenticate Google Analytics API requests. I already tried using the Flow library, and I got the same error.

英文:

I'm using OAuth2 flow to get access to a Google Ads Account. My application runs in a flask server, so I can get the authorization code from the OAuth2 flow using the the requestvariable in the flask context. First, I get the permission screen url, that redirects to "/google_ads/authorized" flask route.

auth_url = 'https://accounts.google.com/o/oauth2/auth'
client_id = os.environ['CLIENT_ID']
client_secret = os.environ['CLIENT_SECRET']
redirect_uri = os.environ['REDIRECT_URI']
scope = 'https://www.googleapis.com/auth/adwords'

In the redirected flask route I'm making a request to exchange the authorization code by the access token and refresh token.

token_url = 'https://accounts.google.com/o/oauth2/token'
payload = {
 "code": request.args['code'],
 "client_id": client_id,
 "client_secret": client_secret,
 "redirect_uri": redirect_uri,
 "grant_type": "authorization_code"
}
req = requests.post(url=token_url, data=json.dumps(payload)
res = req.json()

credentials = {
 'access_token': res['access_token'], 
 'refresh_token': res['refresh_token'], 
 'client_id': client_id,
 'client_secret': client_secret, 
 'expiry': '2023-06-12T18:01:59', 
 'scopes': ['https://www.googleapis.com/auth/adwords']
}

Finally, I use the credentials dict to build a Credentials object, that is passed with the developer token as an argument to instantiate the GoogleAdsClient,

from google.oauth2.credentials import Credentials
from google.ads.googleads.client import GoogleAdsClient

developer_token = os.environ['DEVELOPER_TOKEN']
credentials=Credentials.from_authorized_user_info(creds)
client = GoogleAdsClient(credentials=credentials, developer_token=developer_token)

customer_service = client.get_service("CustomerService")
customer_service.list_accessible_customers()

When I try to make a request and list the available customers in the account, using customer_service.list_accessible_customers(), I got the following error:

{
 "error": {
   "code": 403,
   "message": "The caller does not have permission",
   "status": "PERMISSION_DENIED",
   "details": [
     {
      "@type": "type.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure",
      "errors": [
	 {
	   "errorCode": {
	      "authorizationError": "DEVELOPER_TOKEN_PROHIBITED"
	   },
	   "message": "Developer token is not allowed with project '685646918301'."
         }
       ],
       "requestId": "hYMHcD_FbdI0vwYeKRrAbw"
      }
    ]
  }
}

This method is the same method that I'm using to authenticate Google Analytics API requests. I already tried using the flow lib and I got the same error.

答案1

得分: 0

错误提示表明您曾在过去的同一GCP项目中使用不同的Google Ads API开发者令牌。

一旦您使用特定的GCP项目执行第一个Google Ads API调用,在该请求中使用的开发者令牌将永久与该项目关联

解决此问题的最简单方法是在Google Cloud控制台中创建一个新项目,向该项目添加一个OAuth2客户端,然后使用您当前的令牌与该客户端一起使用。

英文:

The error indicates that you used a different Google Ads API developer token with the same GCP project in the past.

Once you perform your first Google Ads API call with a given GCP project, the developer token used in that request is permanently associated with that project.

Easiest way to solve this is to create a new project in the Google Cloud console, add a OAuth2 client to that project and then use your current token with that client.

huangapple
  • 本文由 发表于 2023年6月13日 05:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定