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

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

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.

  1. auth_url = 'https://accounts.google.com/o/oauth2/auth'
  2. client_id = os.environ['CLIENT_ID']
  3. client_secret = os.environ['CLIENT_SECRET']
  4. redirect_uri = os.environ['REDIRECT_URI']
  5. 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.

  1. token_url = 'https://accounts.google.com/o/oauth2/token'
  2. payload = {
  3. "code": request.args['code'],
  4. "client_id": client_id,
  5. "client_secret": client_secret,
  6. "redirect_uri": redirect_uri,
  7. "grant_type": "authorization_code"
  8. }
  9. req = requests.post(url=token_url, data=json.dumps(payload))
  10. res = req.json()
  11. credentials = {
  12. 'access_token': res['access_token'],
  13. 'refresh_token': res['refresh_token'],
  14. 'client_id': client_id,
  15. 'client_secret': client_secret,
  16. 'expiry': '2023-06-12T18:01:59',
  17. 'scopes': ['https://www.googleapis.com/auth/adwords']
  18. }

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.

  1. from google.oauth2.credentials import Credentials
  2. from google.ads.googleads.client import GoogleAdsClient
  3. developer_token = os.environ['DEVELOPER_TOKEN']
  4. credentials = Credentials.from_authorized_user_info(creds)
  5. client = GoogleAdsClient(credentials=credentials, developer_token=developer_token)
  6. customer_service = client.get_service("CustomerService")
  7. 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:

  1. {
  2. "error": {
  3. "code": 403,
  4. "message": "The caller does not have permission",
  5. "status": "PERMISSION_DENIED",
  6. "details": [
  7. {
  8. "@type": "type.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure",
  9. "errors": [
  10. {
  11. "errorCode": {
  12. "authorizationError": "DEVELOPER_TOKEN_PROHIBITED"
  13. },
  14. "message": "Developer token is not allowed with project '685646918301'."
  15. }
  16. ],
  17. "requestId": "hYMHcD_FbdI0vwYeKRrAbw"
  18. }
  19. ]
  20. }
  21. }

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.

  1. auth_url = 'https://accounts.google.com/o/oauth2/auth'
  2. client_id = os.environ['CLIENT_ID']
  3. client_secret = os.environ['CLIENT_SECRET']
  4. redirect_uri = os.environ['REDIRECT_URI']
  5. 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.

  1. token_url = 'https://accounts.google.com/o/oauth2/token'
  2. payload = {
  3. "code": request.args['code'],
  4. "client_id": client_id,
  5. "client_secret": client_secret,
  6. "redirect_uri": redirect_uri,
  7. "grant_type": "authorization_code"
  8. }
  9. req = requests.post(url=token_url, data=json.dumps(payload)
  10. res = req.json()
  11. credentials = {
  12. 'access_token': res['access_token'],
  13. 'refresh_token': res['refresh_token'],
  14. 'client_id': client_id,
  15. 'client_secret': client_secret,
  16. 'expiry': '2023-06-12T18:01:59',
  17. 'scopes': ['https://www.googleapis.com/auth/adwords']
  18. }

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,

  1. from google.oauth2.credentials import Credentials
  2. from google.ads.googleads.client import GoogleAdsClient
  3. developer_token = os.environ['DEVELOPER_TOKEN']
  4. credentials=Credentials.from_authorized_user_info(creds)
  5. client = GoogleAdsClient(credentials=credentials, developer_token=developer_token)
  6. customer_service = client.get_service("CustomerService")
  7. 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:

  1. {
  2. "error": {
  3. "code": 403,
  4. "message": "The caller does not have permission",
  5. "status": "PERMISSION_DENIED",
  6. "details": [
  7. {
  8. "@type": "type.googleapis.com/google.ads.googleads.v14.errors.GoogleAdsFailure",
  9. "errors": [
  10. {
  11. "errorCode": {
  12. "authorizationError": "DEVELOPER_TOKEN_PROHIBITED"
  13. },
  14. "message": "Developer token is not allowed with project '685646918301'."
  15. }
  16. ],
  17. "requestId": "hYMHcD_FbdI0vwYeKRrAbw"
  18. }
  19. ]
  20. }
  21. }

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:

确定