0
$profileID = *******;
$id = ********;

$accesstoken = 'Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$opts = array(
    "http" => array(
     "method" => "GET",
      'header' => 'Authorization: Bearer '.$accesstoken.
              'Accept: application/json'
   )
 );

$context = stream_context_create($opts);
$url =  'https://www.googleapis.com/dfareporting/v3.3/userprofiles/'.$profileID.'/campaigns/'.$id;

$response = file_get_contents($url,false,$context);

echo '<pre>';
print_r($response);
echo '</pre>';

I've read the documentation, I chosen to use the Authorization Bearer option in the header. All I get in return is 'Warning: file_get_contents(https://www.googleapis.com/dfareporting/v3.3/userprofiles/XXXXXX/campaigns/XXXXXXXX): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized in /my/file/path/file.php on line 93'

I'm just trying to return a campaign by id.

I have my oauth2 set up. I've returned information using the sample files and examples but I don't feel like reverse engineering many php scripts just to get impressions of some campaign. I have a tokenStore.json with my access token. I've used composer to get a vendor folder and client secrets. I've been connecting to many the "Getting Started" examples ranging from Google Analytics to Display & Video 360 but I NEVER figured out how to utilize these types of pages. Which seems way more useful than deconstructing the OOP hierarchy these Google fellas make.

My access token is valid and I don't need an API key if I chose this way to getting a HTTP request. I can run GetCampaigns.php right now and I'll get a correct answer.

When trying the cURL option with:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, $opts);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);   
curl_close($ch);  

echo '<pre>';
print_r($output);
echo '</pre>';

I get a json object saying:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

Please any idea would help.

Vin...C
  • 1
  • 6

1 Answers1

0

So I figured it out. The code before the code I posted above was this:

if (file_exists(__DIR__ ."/tokenStore_DCM.json") && filesize(__DIR__ ."/tokenStore_DCM.json") > 0) {

    $accessToken = json_decode(file_get_contents(__DIR__ ."/tokenStore_DCM.json"), true);

    $client->setAccessToken($accessToken);

} else {
    // If no cached credentials were found, authorize and persist
    // credentials to the token store.
    print 'Open this URL in your browser and authorize the application.';
    printf("\n\n%s\n\n", $client->createAuthUrl());
    print 'Enter the authorization code: ';
    $code = trim(fgets(STDIN));
    $client->authenticate($code);

    file_put_contents(__DIR__ ."/tokenStore_DCM.json", json_encode($client- >getAccessToken()));
}

This snippet I followed is from this page in the 'Make a request' section of PHP. I am using a amazon ec2 instance running a form of Redhat Linux. I simply had to delete the 'tokenStore_DCM.json' I created, run the code on the page above again. Then, paste the url they give me into my URL, accept the scopes, and then paste the auth code they gave me back into my CLI when the prompt shows. Next time I ran the code above it updated my TokenStore file with a new access_token.

The Curl route is more reliable choice and is the one that works. Make sure not to append the access token with a dot. For some reason it doesn't work, here is the lines I used that worked:

$authorization = "Authorization: Bearer xxxx.XX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxNumbersAndLettersxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

//(say you have a $accesstoken var) do not do this :
//$authorization = "Authorization: Bearer '.accesssToken;


$url ='https://content.googleapis.com/dfareporting/v3.3/userprofiles/'.$profileID.'/campaigns/'.$id;


$header = array('GET','Content-Type: application/json' , $authorization );

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HTTPHEADER,$header);  

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);   

curl_close($ch);  

echo '<pre>';
print_r($output);
echo '</pre>';
Vin...C
  • 1
  • 6