0
votes

i want to get access token from mailchimp oauth2 but in every try i was fail. Here i used guzzle and my actual request is

curl --request POST \ --url 'https://login.mailchimp.com/oauth2/token' \ --data "grant_type=authorization_code&client_id={client_id}&client_secret={client_secret}&redirect_uri={encoded_url}&code={code}" \ --include

i already try curl and javascript but javascript request is not accepted by mailchimp this is code i used:

$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2', 
 'defaults' => [
                'exceptions' => false ],
    'header' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ],
    'verify' => false
    ]);
    $result = $http->request('POST','/token', [
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => $this->client_id,
        'client_secret' => $this->client_secret,
        'redirect_uri' => $this->redirect_uri,
        'code' => $code,
        ],
    ]);
    $response = $result->send();
    $responseBody = $response->getBody(true);
    var_dump($responseBody);
    die();

the expected actual result is: "access_token":"5c6ccc561059aa386da9d112215bae55","expires_in":0,"scope":null but my error Client error: POST https://login.mailchimp.com/token resulted in a 404 Not Found response:

1

1 Answers

0
votes

Due to the way Guzzle works, you are using an absolute URI.

First you set the base_uri:

$http = new Client(['base_uri' => 'https://login.mailchimp.com/oauth2']);

Then make the request:

$http->request('POST', '/token')

Because you've added the leading slash, Guzzle will treat it has an absolute URI and hit https://login.mailchimp.com/token, as it is doing. My guess is the desired endpoint to hit is https://login.mailchimp.com/oauth2/token

This can be fixed by adding a trailing slash to the base_uri and removing the leading slash when performing the request.

Info from the docs: http://docs.guzzlephp.org/en/stable/quickstart.html#creating-a-client

It follows RFC3986