How to make a POST request via Node JS to fetch data from the Google Ads API?

1,214 views
Skip to first unread message

anirud...@gmail.com

unread,
May 6, 2019, 6:53:54 AM5/6/19
to AdWords API and Google Ads API Forum
I'm confused with the documentation. Could someone guide me on how to make requests via REST API. 

Here is what I have tried. 


                  
var options = {
                        url
: 'https://googleads.googleapis.com/v1/customers/'+accountid+'/adGroups',
                        headers
: {
                               
'Authorization': bearerToken,
                               
'Accept': 'application/json',
                               
'Content-Type': 'application/json',
                     
'login-customer-id': '123456789',
                     
'developer-token': 'ABCDEFGH'
                       
}
                     
};


                  request
.post(options, function(err, response) {
console
.log(response);                
)};


I keep getting 404 error. I need to fetch different kinds of data based on a date range. 
Example: List of Campaigns, Total Clicks, Total Impressions, Average CPC, Regionwise Performance. 

How should the URL be formed, where can I find a proper guide? Their official documentation is very confusing for me. 





googleadsapi...@google.com

unread,
May 6, 2019, 10:44:44 AM5/6/19
to AdWords API and Google Ads API Forum
Hello Anirudh,

In order to get the data and metrics from your account you can use the GoogleAdsService with the help of Google Ads Query Language guide. The URL for all search requests will be same as mentioned in below example. In the below sample query I am getting campaign details for 1 month. please give it a try and let me know if you have any further questions. Also, this Interactive Google Ads Query Builder will help you in building the search queries.

Sample CURL operation below:
curl -H "Authorization: Bearer ACCESS_TOKEN_HERE
       -H "developer-token: DEVELOPER_TOKEN_HERE
       -H "login-customer-id: LOG_IN_CUSTOMER_ID
       -H "Content-Type: application/json" https://googleads.googleapis.com/v1/customers/CLIENT_CUSTOMER_ID_HERE/googleAds:search --data '{query: "SELECT campaign.id, campaign.name, metrics.clicks, metrics.impressions, metrics.conversions, metrics.cost_micros FROM campaign WHERE segments.date DURING LAST_30_DAYS ORDER BY campaign.id"}'

Regards,
Sai Teja, Google Ads API Team


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

Was your question answered? Please rate your experience with us by taking a short survey.
If not -- reply to this email and tell us what else we can do to help.

Take Survey

Also find us on our blog and discussion group:
http://googleadsdeveloper.blogspot.com/search/label/adwords_api
https://developers.google.com/adwords/api/community/

--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
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 adwor...@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscribe@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 "AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-api+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-api/56828f9c-b61d-43ae-88c5-52aeeeb82b49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

anirudh maddy

unread,
May 8, 2019, 10:12:21 AM5/8/19
to AdWords API and Google Ads API Forum
headers: {
'Authorization': bearerToken,
'Accept': 'application/json',
'Content-Type': 'application/json',
'login-customer-id': logincustomerid,
'developer-token': 'developerToken',

},
data: { 'query': 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id' }
};


request.post(options, function(err, response) {
console.log(response.body);
}


I'm getting the below error with this code. 

 body:
   '{\n  "error": {\n    "code": 401,\n    "message": "Request is missing required authentication credential. Expected OAuth 2 acces
s token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-pr
oject.",\n    "status": "UNAUTHENTICATED",\n    "details": [\n      {\n        "@type": "type.googleapis.com/google.ads.googleads.v1
.errors.GoogleAdsFailure",\n        "errors": [\n          {\n            "errorCode": {\n              "authenticationError": "AUTH
ENTICATION_ERROR"\n            },\n            "message": "Authentication of the request failed."\n          }\n        ]\n      }\n
    ]\n  }\n}\n' }


googleadsapi...@google.com

unread,
May 8, 2019, 12:05:42 PM5/8/19
to AdWords API and Google Ads API Forum
Hello Anirudh,

It seems like the Access token is not passed properly. In the API call the Authorization header should be passes as shown below. Please give it a try, if you are still having issues, you could generate a new access token by following this guide.

'Authorization': Bearer AccesToken_pater_here_without_extra_spaces_or_chars,


Regards,
Sai Teja, Google Ads API Team

=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

anirudh maddy

unread,
May 9, 2019, 2:13:21 AM5/9/19
to AdWords API and Google Ads API Forum
In followed the exact same instructions, generated AccessToken using your Guide, but I'm still getting the same error. 

googleadsapi...@google.com

unread,
May 9, 2019, 3:24:27 PM5/9/19
to AdWords API and Google Ads API Forum
Hello Anirudh,

looks like there is another thread going on for the authorization issue you are facing. Let's continue our discussion on that thread to avoid confusion and duplicacy.


Regards,
Sai Teja, Google Ads API Team

=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    https://ads-developers.googleblog.com/search/label/google_ads_api
    https://developers.google.com/adwords/api/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Reply all
Reply to author
Forward
0 new messages