问题描述
我在Postman上有有效的代码,可以获取所有虚拟主机,但是当我用Guzzle在PHP上进行操作时,这给我带来了麻烦.在代码的第一部分中,我在获得承载令牌的地方获取了它,但是在获取虚拟主机列表时,却给了我错误.
I have working code on Postman and I can get all virtual hosts,but when Im doing on the PHP with Guzzle it gives me trouble.First part of code I get it where I get bearer token, but then to get list of virtual hosts it gives me error.
这是我的代码
$client = new \GuzzleHttp\Client();
$res = $client->request(
'POST',
"https://login.microsoftonline.com/".$tenant_id."/oauth2/token",
Array(
'form_params' => Array(
'grant_type' => 'client_credentials',
'client_id' => $client_id,
'client_secret' => $client_secret,
'resource' => 'https://management.core.windows.net/',
)
)
);
$body = $res->getBody();
$stringBody = $body;
$body_json = json_decode($stringBody);
$r = $client->request(
'GET',
'https://management.azure.com/subscriptions/'.$subscriptionId.'/providers/Microsoft.Compute/virtualMachines?api-version=2017-12-01', [
['headers' => [
'Authorization' => 'Bearer '.$body_json->access_token
]
],
'debug' => true
]);
和错误:
WWW-Authenticate: Bearer authorization_uri="https://login.windows.net/myid", error="invalid_token", error_description="The authentication failed because of missing 'Authorization' header."
我不明白为什么它不想连接
I don't see why it doesn't want to connect
更新:此工作原理,但使用了CURL
UPDATE: THIS WORKS, but with CURL
$authorization = "Authorization: Bearer ". $body_json->access_token;
$ch = curl_init('https://management.azure.com/subscriptions/'.$subscriptionId.'/providers/Microsoft.Compute/virtualMachines?api-version=2017-12-01');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result));
更新:列出虚拟机不起作用,它给我空的结果,我是否需要授予另一个访问权限才能执行此操作? https://management.azure.com/subscriptions/'.$ subscriptionId.'/providers/Microsoft.Compute/virtualMachines?api-version = 2017-12-01
UPDATE:listing Virtual machines don't work, it gives me empty results, do I need to grant another access to be able to do this?https://management.azure.com/subscriptions/'.$subscriptionId.'/providers/Microsoft.Compute/virtualMachines?api-version=2017-12-01
推荐答案
请使用以下代码进行请求:
Please use this code to request:
['headers' => [
'Authorization' => "Bearer " . $body_json->access_token
]
这篇关于在天青上大吃一惊,以获取所有虚拟主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!