1

We're moving from Guzzle3 to Guzzle6. I wrote a process to authenticate and access the Azure Management API that worked fine in Guzzle3. However, I'm unable to figure out how to get it to work in Guzzle6. The purpose is to get the access token which is then use in subsequent requests to the Azure Management API.

Original Code:

$client = new Guzzle\Http\Client();
$request = $client->post("https://login.microsoftonline.com/{$tenant_id}/oauth2/token",
    Array(
        'Accept' => 'application/json',
    ),
    Array(
        'grant_type'    => 'client_credentials',
        'client_id'     => $application_id,
        'client_secret' => $application_secret,
        'resource'      => 'https://management.core.windows.net/',
    )
);

$response = $request->send();
$body = $response->getBody(true);

New code I'm working on:

$client = new GuzzleHttp\Client();
$response = $client->request(
    'POST',
    "https://login.microsoftonline.com/{$tenant_id}/oauth2/token",
    Array(
        GuzzleHttp\RequestOptions::JSON => Array(
            'grant_type'    => 'client_credentials',
            'client_id'     => $application_id,
            'client_secret' => $application_secret,
            'resource'      => 'https://management.core.windows.net/',
        )
    )
);

I've tried so many variations with no luck. I would be grateful for any insight anyone could provide.

Thanks!

4

1 回答 1

2

好吧,我想在这里发帖有助于指导我对此的想法。我能够让它工作。对于同一条船上的其他人,这是我想出的解决方案:

$client = new GuzzleHttp\Client();
$response = $client->request(
    'POST',
    "https://login.microsoftonline.com/{$tenant_id}/oauth2/token",
    Array(
        'form_params' => Array(
            'grant_type'    => 'client_credentials',
            'client_id'     => $application_id,
            'client_secret' => $application_secret,
            'resource'      => 'https://management.core.windows.net/',
        )
    )
);
于 2017-06-06T05:17:06.173 回答