Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对Google DCM/DFA API的未经授权HTTP请求_Php_Api_Google Api Php Client_Unauthorized - Fatal编程技术网

Php 对Google DCM/DFA API的未经授权HTTP请求

Php 对Google DCM/DFA API的未经授权HTTP请求,php,api,google-api-php-client,unauthorized,Php,Api,Google Api Php Client,Unauthorized,任何想法都会有帮助 所以我想出来了。我在上面发布的代码之前的代码是: $authorization = "Authorization: Bearer xxxx.XX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxNumbersAndLettersxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //(say you have a $accesstoken var) do not

任何想法都会有帮助

所以我想出来了。我在上面发布的代码之前的代码是:

$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>';
我遵循的这个片段来自PHP的“发出请求”部分。我正在使用一个运行Redhat Linux的AmazonEC2实例。我只需删除我创建的“tokenStore_DCM.json”,然后再次运行上面页面上的代码。然后,将他们提供给我的url粘贴到我的url中,接受作用域,然后在提示显示时将他们提供给我的身份验证代码粘贴回我的CLI中。下次我运行上面的代码时,它用一个新的访问令牌更新了我的令牌库文件

卷曲路径是更可靠的选择,也是最有效的选择。确保不要在访问令牌上附加点。由于某些原因,它不起作用,以下是我使用的行:

$authorization=“授权:持票人xxxx.XX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
//(假设您有$accesstoken变量)不要这样做:
//$authorization=“authorization:bearier”.accesssToken;
$url='1https://content.googleapis.com/dfareporting/v3.3/userprofiles/“.$profileID./campetings/”.$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\u exec($ch);
卷曲关闭($ch);
回声';
打印(输出);
回声';
$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>';
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}
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()));
}
$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>';