googleads / googleads-php-lib

Google Ad Manager SOAP API Client Library for PHP
Apache License 2.0
656 stars 769 forks source link

REST API Get request to generate KeywordPlanHistoricalMetrics in PHP #731

Closed MOSTAFAEZZAT closed 2 years ago

MOSTAFAEZZAT commented 2 years ago

I want to use REST API of Google Ads API by GET request to generate Metrics for keyword I'm following this doc https://developers.google.com/google-ads/api/rest/overview when I use this piece of code it prints this error message

  //The url you wish to send the POST request to
        $url = "https://googleads.googleapis.com/v9/{DigitalMarketing=customers/*/keywordPlans/*}:generateHistoricalMetrics=competition,avgMonthlySearches,competitionIndex,lowTopOfPageBidMicros,highTopOfPageBidMicros, monthlySearchVolumes";
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);
       print_r( $response);

ssdssdfdf

Vijaysinh commented 2 years ago

@MOSTAFAEZZAT you are using wrong url you need to update url (*) values with proper ids

i.e.

https://googleads.googleapis.com/{{API_VERSION}}/customers/{{ADWORDS_ACCOUNT_ID}}/keywordPlans/{{KEYWORDPLANID}}: generateHistoricalMetrics

MOSTAFAEZZAT commented 2 years ago

@MOSTAFAEZZAT you are using wrong url you need to update url (*) values with proper ids

i.e.

https://googleads.googleapis.com/{{API_VERSION}}/customers/{{ADWORDS_ACCOUNT_ID}}/keywordPlans/{{KEYWORDPLANID}}: generateHistoricalMetrics

Thanks so much for responding I did edit it but with this url was suggested by google ads api forum $ch =curl_init("https://googleads.googleapis.com/v9/customers/(851-978-1266)/keywordPlans/(1):generateHistoricalMetrics" );

I changed the code to your url version prints the same error in the issue

$keyword= "Digital Maketing";
        $url =  "https://googleads.googleapis.com/{{v9}}/customers/{{(851-978-1266}}/keywordPlans/{{1}}: generateHistoricalMetrics"  ;
        $ch =curl_init("https://googleads.googleapis.com/v9/customers/(851-978-1266)/keywordPlans/(1):generateHistoricalMetrics" );

        curl_setopt($ch, CURLOPT_HEADER, true) ;
        curl_setopt($ch, CURLOPT_POSTFIELDS, $keyword); 

        curl_setopt($ch, CURLOPT_POSTFIELDS, true); 

        $respose = curl_exec($ch);
        print_r( $respose);

but still the problem exist and all i just need is to get the historical metrics for a word through Rest Interface with Google Ads API and I really have confused about all the stuff

HTTP/2 401 www-authenticate: Bearer realm="https://accounts.google.com/" vary: X-Origin vary: Referer vary: Origin,Accept-Encoding content-type: application/json; charset=UTF-8 date: Wed, 29 Dec 2021 12:21:11 GMT server: ESF cache-control: private x-xss-protection: 0 x-frame-options: SAMEORIGIN x-content-type-options: nosniff alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43" accept-ranges: none { "error": { "code": 401, "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.", "status": "UNAUTHENTICATED", "details": [ { "@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "CREDENTIALS_MISSING", "domain": "googleapis.com", "metadata": { "method": "google.ads.googleads.v9.services.KeywordPlanService.GenerateHistoricalMetrics", "service": "googleads.googleapis.com" } } ] } } 1
MOSTAFAEZZAT commented 2 years ago

@fiboknacky I'm sorry to bother you may you help in implementing Rest Interfance to use generate Historical Keywords metrics in the back end of the website. I tried this code to turn curl from command line into the server side also it's same result not working

Curl Command line

curl -i --request POST https://googleads.googleapis.com/v9/customers/(ACCOUNT NUMBER):generateKeywordIdeas \
--header "Content-Type: application/json" \
--header "login-customer-id: (MCC ID)" \
--header "developer-token: (DEVELOPER TOKEN" \
--header "Authorization: Bearer (ACCESS TOKEN)" \
--data '{

"keywordSeed": {
    "keywords": [
    "cofee"
  ]
  }
}'

Curl Server Side

 $keyword = "Digital Maketing";
        $ch = curl_init("https://googleads.googleapis.com/v9/customers/{8519781266}:generateKeywordIdeas");
        $headers = [
            "Content-Type: application/json",
            "developer-token: Af8L57gifnGl1b4M324_Tg",
            "login-customer-id: 2260416591",
            "Authorization: Bearer 847786194979-rs68tcknahpj0psa5e6l8sosrsu0jrbl.apps.googleusercontent.com"
        ];
        curl_setopt($ch, CURLOPT_POSTFIELDS, $keyword);

        curl_setopt($ch, CURLOPT_HEADER, $headers);

        $respose = curl_exec($ch);
        print_r($respose);
fiboknacky commented 2 years ago

This library is for the AdWords API. I saw you also already opened an issue in the google-ads-php repository, so I'll close this one. For non-library issues and questions (e.g., REST API), I'd appreciate if you could discuss on the Google Ads API forum as well. Thanks.