向Google DCM/DFA API发出未经授权的HTTP请求


$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>';

我已经阅读了文档,我选择在标题中使用Authorization Bearer选项。我得到的只是"警告:file_get_contents"(https://www.googleapis.com/dfareporting/v3.3/userprofiles/XXXXXX/campaigns/XXXXXXXX):无法打开流:HTTP请求失败!HTTP/1.0 401在第93行的/my/file/path/file.php中未经授权的

我只是想通过id返回一个活动。

我已经设置好了我的oauth2。我已经使用示例文件和示例返回了信息,但我不想仅仅为了获得一些活动的印象而对许多php脚本进行逆向工程。我有一个带有访问令牌的tokenStore.json。我使用composer来获取供应商文件夹和客户机密。我已经连接到许多"入门"的例子,从谷歌分析到显示&视频360,但我从未想过如何利用这些类型的页面。这似乎比这些谷歌家伙解构OOP层次结构更有用。

我的访问令牌是有效的,如果我选择这种方式来获取HTTP请求,我不需要API密钥。我现在可以运行GetCampaigns.php,我会得到正确的答案。

尝试cURL选项时使用:

$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>';

我得到一个json对象,上面写着:

{
"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("nn%snn", $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()));
}

我关注的这个片段来自PHP的"发出请求"部分的这个页面。我使用的是一个运行Redhat Linux形式的amazon ec2实例。我只需要删除我创建的"tokenStore_DCM.json",再次运行上面页面上的代码。然后,将他们提供给我的url粘贴到我的url中,接受作用域,然后在提示显示时将他们提供的身份验证代码粘贴回我的CLI中。下次我运行上面的代码时,它用一个新的access_token更新了我的TokenStore文件。

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

$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>';

最新更新