daviddesberg / PHPoAuthLib

PHP 5.3+ oAuth 1/2 Client Library
Other
1.09k stars 454 forks source link

Instagram access token request returns 400 Bad Request #421

Open Rixot opened 9 years ago

Rixot commented 9 years ago

Hi,

I'm using this library for a number of services including Instagram, but I'm having trouble with Instagram and requestAccessToken():

file_get_contents(https://api.instagram.com/oauth/access_token): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST on line 73 in [...]/vendor/lusitanian/oauth/src/OAuth/Common/Http/Client/StreamClient.php

As far as I can tell, the request body is correct:

'code' => string 'f47258f0ad064abda528c127af1a47ed' (length=32)
'client_id' => string '(( client id ))' (length=32)
'client_secret' => string '(( client secret ))' (length=32)
'redirect_uri' => string '(( redirect uri ))' (length=151)
'grant_type' => string 'authorization_code' (length=18)

The client ID, client secret and redirect URI are all correct and match the application credentials. I know the HTTP client in this library also defaults to making POST requests, so as far as I can tell, I'm doing everything correctly and the workflow matches the one outlined in the Instagram docs.

Note that I have a working implementation of Twitter auth using the same library in the same project. Both are handled by the same class method, which I based on the examples:

if (!empty($_GET['oauth_token'])) {
    // OAuth 1 assumed; an extra step is required to obtain the access token
    $token = $this->storage->retrieveAccessToken($service);
    $service->requestAccessToken(
        $_GET['oauth_token'],
        $_GET['oauth_verifier'],
        $token->getRequestTokenSecret()
    );
} elseif (!empty($_GET['code'])) {
    // OAuth 2 assumed; obtain the access token
    $state = isset($_GET['state']) ? $_GET['state'] : null;
    $service->requestAccessToken($_GET['code'], $state);
}

Any ideas?

elliotchance commented 9 years ago

Which version of this library are you using?

Rixot commented 9 years ago

0.5.5 On Sep 22, 2015 00:26, "Elliot Chance" notifications@github.com wrote:

Which version of this library are you using?

— Reply to this email directly or view it on GitHub https://github.com/Lusitanian/PHPoAuthLib/issues/421#issuecomment-142136723 .

Irohane commented 8 years ago

Late reply, but maybe it will help others. The cause is probably invalid settings of credentials (if you based on example)

$credentials = new Credentials(
    $servicesCredentials['instagram']['key'],
    $servicesCredentials['instagram']['secret'],
    '<ABSOLUTE_URI>' // $currentUri->getAbsoluteUri() add GET params and that is causing this problem
);

so replace $currentUri->getAbsoluteUri() with EXACT uri as you configured in your application and it should work.

nbyloff commented 8 years ago

I am actually having this problem as well on Instagram only, and tried using the absolute URI. Getting the same error as the OP.

$credentials = new Credentials(
        getenv('INSTAGRAM_APP_KEY'),
        getenv('INSTAGRAM_APP_SECRET'),
        getenv('INSTAGRAM_ABSOLUTE_URI')
    );

Edit This was mostly likely to a bug on instagram for the past 24 hours. Seems fixed.