0

I have contacted Google API for request to pull the data from adwords account .

They suggested use below format of PHP code to call from Rest API.

#!/bin/bash
ENDPOINT='https://googleads.googleapis.com'
VERSION='v3'                                                                    
CID=6602583583                                                                  

QUERY='
  SELECT
    campaign.advertising_channel_type,
    metrics.all_conversions,
    metrics.cost_per_conversion,
    metrics.impressions,
    metrics.interactions
  FROM campaign                                                                   
'

curl -v --header "Authorization: Bearer $ACCESS_TOKEN" --header "developer-token: $DEV_TOKEN" --data query="$QUERY" $PROD_ENDPOINT/$VERSION/customers/$CID/googleAds:search.

I tried below format of Rest resource to call the google adwords API .I am not sure whether i am sending right request same as above like PHP.

When I run below code I am getting error "status not found".

Http http = new Http();
    HttpRequest httpReq = new HttpRequest();
    HttpResponse httpRes = new HttpResponse();
    String Query='SELECT campaign.advertising_channel_type, metrics.all_conversions, metrics.cost_per_conversion,metrics.impressions,  metrics.interactions FROM campaign';
    httpReq.setEndpoint('https://googleads.googleapis.com/v3/customers/6602583583/googleAds:search');
    httpReq.setMethod('POST');
    httpReq.setHeader('developer-token', 'developertoken');
    httpReq.setHeader('Authorization','Bearer+Access token');
    httpReq.setHeader('login-customer-id', 'customerid');
    httpReq.setHeader('Accept', 'application/json');
    httpReq.setHeader('Content-Type', 'application/json');
    httpReq.setHeader('Host', 'googleads.googleapis.com');
    httpReq.setHeader('Content-Length', '784');
    httpReq.setBody('Query');
    httpRes = (new Http()).send(httpReq);
        if(httpRes.getStatusCode() == 200){
            string response1= httpRes.getBody();
            System.debug('#### HtteRes '+HttpRes.getBody());
            Map<String, Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(response1);
         system.debug('response map values to verify'+ responseMap);   
        } 
0

From curl docs:

When the data is sent by a browser after data have been filled in a form, it will send it "URL encoded", as a serialized name=value pairs separated with ampersand symbols (&). You send such data with curl's -d or --data option

The equivalent in Apex is

req.setBody('query='+EncodingUtil.urlEncode(Query, 'UTF-8'));
3
  • Hi Identigral, Thank you fro your answer.I changed the code tried again but still getting same error. Mar 13 '20 at 16:04
  • The only remaining difference between PHP/Curl example and your Apex headers are extra headers Host, Content-Length and login-customer-id. You can try removing them and see if it makes a difference. This is likely not a Salesforce-side issue. Google Ads (REST) API is in beta status and the doc pages aren't even responding right now. If it still doesn't work, recommend you switch to the officially supported Google AdWords (SOAP) API.
    – identigral
    Mar 13 '20 at 16:31
  • Thanks for you suggestion .We just need to remove HOSt and Content Length .the login-customer-id. required to fetch the data. Mar 16 '20 at 11:44
0
Http http = new Http();
HttpRequest httpReq = new HttpRequest();
HttpResponse httpRes = new HttpResponse();
String Query='SELECT campaign.advertising_channel_type,campaign.resource_name, metrics.all_conversions, metrics.cost_per_conversion,metrics.impressions,metrics.interaction_rate ,metrics.interactions,metrics.conversions_from_interactions_rate,metrics.cost_per_all_conversions FROM campaign';
httpReq.setEndpoint('https://googleads.googleapis.com/v3/customers/customerid/googleAds:search');
httpReq.setMethod('POST');
httpReq.setHeader('developer-token', 'developer token');
httpReq.setHeader('Authorization','Bearer + Access token');
httpReq.setHeader('login-customer-id', 'customerid');
httpReq.setBody('query='+EncodingUtil.urlEncode(Query, 'UTF-8'));
httpRes = (new Http()).send(httpReq);
    if(httpRes.getStatusCode() == 200){
        string response1= httpRes.getBody();
        System.debug('#### HtteRes '+HttpRes.getBody());
        Map<String, Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(response1);
     system.debug('response map values to verify'+ responseMap);   
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.