繁体   English   中英

吞噬天青以获得所有虚拟主机

[英]guzzle on azure to get all virtual hosts

我在Postman上有工作代码,可以获取所有虚拟主机,但是当我用Guzzle在PHP上执行操作时,这给我带来了麻烦。 在代码的第一部分中,我在获得承载令牌的地方获取了代码,但是在获取虚拟主机列表时,却出现了错误。

这是我的代码

$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."

我不明白为什么它不想连接

更新:这项工作,但使用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

请使用此代码请求:

 ['headers' => [
                'Authorization' => "Bearer " . $body_json->access_token
            ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM