NOT_ADS_USER when running python script

2023-05-15 Thread Enzo Pucci


Dear Google Ads API Team,

 

I am trying to run several Python scripts to get dashboards or other infos 
from my campaigns, but had no success in almost every one.

 

Also, I've already been conceived as an Administrator in my company's MCC 
and in the specific Account which we want to be the reference for our 
scripts and to get information from.

 

To run my scripts, I've reunited MCC_id, Account_id, Client_id, 
Client_secret, refresh_token and developer_token like this:

 

- MCC_id: In the google ads interface
- Account_id: Also in google ads interface, but logged in the account 
we want;
- Client_id: Google ads interface from the specific company
- client_secret/secret_key and refresh token: Logging into the OAuth 
interface.

 

So, when i ran this Python script, it actually worked and we got the name 
of all campaigns:



import argparse
import sys

 

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
from dotenv import dotenv_values

 

config = dotenv_values(".env")
credentials = {
"developer_token": config["developer_token"],
"refresh_token": config["refresh_token"],
"client_id": config["client_id"],
"client_secret": config["client_secret"],
"account_id": config["account_id"],
"mcc_id": config["mcc_id"],
"use_proto_plus": True
}

 

def main(client, customer_id):
ga_service = client.get_service("GoogleAdsService")

 

query = """
SELECT
  campaign.id,
  campaign.name
FROM campaign
ORDER BY campaign.id"""

 

# Issues a search request using streaming.
stream = ga_service.search_stream(customer_id=customer_id, query=query)

 

for batch in stream:
for row in batch.results:
print(
f"Campaign with ID {row.campaign.id} and name "
f'"{row.campaign.name}" was found.'
)

 

if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in 
the
# home directory if none is specified.
google_ads_client = GoogleAdsClient.load_from_dict(credentials, 
version="v13")

 

customer_id = config["account_id"]

 

try:
main(google_ads_client, customer_id)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in 
error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)

But when i tried to catch the data from a campaign using:

 

###
DEVELOPER_TOKEN = "-x"
PATH_TO_CREDENTIALS = "caminhoJson1205.json"
CUSTOMER_ID = "x"

 

def get_google_ads_data(credentials_path, developer_token, customer_id):
try:
# Autenticação
credentials = 
service_account.Credentials.from_service_account_file(credentials_path)
credentiaels = credentials.with_scopes(['
https://www.googleapis.com/auth/adwords'])

 

# Criar cliente do Google Ads
google_ads_client = GoogleAdsClient(credentials=credentials, 
developer_token=developer_token)

 

# Construir a consulta
query = f"""
SELECT
campaign.id
# ad_group.id,
# ad.id,
# ad.type,
# ad.headline,
# ad.description,
# metrics.impressions,
# metrics.clicks,
# metrics.average_cpc
FROM
ad
WHERE
segments.date DURING LAST_7_DAYS
AND metrics.impressions > 0
"""

 

ga_service = google_ads_client.get_service("GoogleAdsService")

# Executar a consulta
response = ga_service.search_stream(customer_id=customer_id, 
query=query)

 

# Exibir resultados
for batch in response:
for row in batch.results:
print(f"Ad ID: {row.ad.id.value}, Headline: 
{row.ad.headline.value}, Impressions: {row.metrics.impressions.value}")

 

except Exception as e:
print(f"An error occurred: {e}")
return None

 

if __name__ == "__main__":
get_google_ads_data(PATH_TO_CREDENTIALS, DEVELOPER_TOKEN, CUSTOMER_ID)

It didn't work.

 

Important to say we also created a service account (according to what GPT 
told us), but even when we tried to connect the service account to the 
project, it didn't work as well.

 

So please, help us step-by-step on how to properly catch campaigns using 
Python.

-- 
-- 
=~=~=~=~=~=

"User in the cookie is not a valid Ads user."

2023-05-04 Thread Enzo Pucci
Hi,

I am trying to run a sample code in Python to use google query language but 
I'm getting the error as above. However, I've already managed to do a 
script that returns all of my campaigns.

*The current script that I'm trying to run is this:*

import google.auth
from google.auth.transport.requests import Request
from google.oauth2 import service_account
from google.ads.googleads.client import GoogleAdsClient

# Configuração
DEVELOPER_TOKEN = "xxx-x"
PATH_TO_CREDENTIALS = "caminhoParaCredenciais.json"
CUSTOMER_ID = "xx" (*I've already tried with a -*)

def get_google_ads_data(credentials_path, developer_token, customer_id):
try:
# Autenticação
credentials = 
service_account.Credentials.from_service_account_file(credentials_path)
credentiaels = 
credentials.with_scopes(['https://www.googleapis.com/auth/adwords'])

# Criar cliente do Google Ads
google_ads_client = GoogleAdsClient(credentials=credentials, 
developer_token=developer_token)

# Construir a consulta
query = f"""
SELECT
campaign.id
# ad_group.id,
# ad.id,
# ad.type,
# ad.headline,
# ad.description,
# metrics.impressions,
# metrics.clicks,
# metrics.average_cpc
FROM
ad
WHERE
segments.date DURING LAST_7_DAYS
AND metrics.impressions > 0
"""

ga_service = google_ads_client.get_service("GoogleAdsService")

# Executar a consulta
response = ga_service.search_stream(customer_id=customer_id, 
query=query)

# Exibir resultados
for batch in response:
for row in batch.results:
print(f"Ad ID: {row.ad.id.value}, Headline: 
{row.ad.headline.value}, Impressions: {row.metrics.impressions.value}")

except Exception as e:
print(f"An error occurred: {e}")
return None

if __name__ == "__main__":
get_google_ads_data(PATH_TO_CREDENTIALS, DEVELOPER_TOKEN, CUSTOMER_ID)

*But I'm getting this return:*
Request made: ClientCustomerId: x, Host: googleads.googleapis.com, 
Method: /google.ads.googleads.v13.services.GoogleAdsService/SearchStream, 
RequestId: rAdWhFeZYtpdmODskp5SVQ, IsFault: True, FaultMessage: User in the 
cookie is not a valid Ads user.
An error occurred: (<_SingleThreadedRendezvous of RPC that terminated with:
status = StatusCode.UNAUTHENTICATED
details = "Request is missing required authentication credential. Expected 
OAuth 2 access token, login cookie or other valid authentication 
credential. See 
https://developers.google.com/identity/sign-in/web/devconsole-project.";
debug_error_string = "UNKNOWN:Error received from peer 
ipv4:142.250.219.10:443 {grpc_message:"Request is missing required 
authentication credential. Expected OAuth 2 access token, login cookie or 
other valid authentication credential. See 
https://developers.google.com/identity/sign-in/web/devconsole-project.";, 
grpc_status:16, created_time:"2023-05-03T17:28:14.912279934+00:00"}"
>, <_SingleThreadedRendezvous of RPC that terminated with:
status = StatusCode.UNAUTHENTICATED
details = "Request is missing required authentication credential. Expected 
OAuth 2 access token, login cookie or other valid authentication 
credential. See 
https://developers.google.com/identity/sign-in/web/devconsole-project.";
debug_error_string = "UNKNOWN:Error received from peer 
ipv4:142.250.219.10:443 {grpc_message:"Request is missing required 
authentication credential. Expected OAuth 2 access token, login cookie or 
other valid authentication credential. See 
https://developers.google.com/identity/sign-in/web/devconsole-project.";, 
grpc_status:16, created_time:"2023-05-03T17:28:14.912279934+00:00"}"
>, errors {
  error_code {
authentication_error: NOT_ADS_USER
  }
  message: "User in the cookie is not a valid Ads user."
}
request_id: "rAdWhFeZYtpdmODskp5SVQ"
, 'rAdWhFeZYtpdmODskp5SVQ')

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/ef68d48c-b24c-4925-8000-83b128584264