unsplash / unsplash-php

👻 Official PHP wrapper for the Unsplash API
MIT License
415 stars 74 forks source link

Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method. #92

Closed ghost closed 6 years ago

ghost commented 6 years ago

When i'm trying to use oath authentication I keep getting this error. I have really no clue what is going wrong.

Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method.

I'm using the script mentioned in the example folder. Specified:

require $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';
session_start();
// Landing on the page for the first time, setup connection with private application details registered with unsplash and redirect user to authenticate
if (!isset($_GET['code']) && !isset($_SESSION['token'])) {
    \Crew\Unsplash\HttpClient::init([
        'applicationId' => 'XXXXX',
        'secret'        => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        'callbackUrl'   => 'http://unsplash-api.dev'
    ]);
    $httpClient = new \Crew\Unsplash\HttpClient();
    $scopes = ['write_user', 'public'];
    //echo $httpClient::$connection->getConnectionUrl($scopes);
    header("Location: ". $httpClient::$connection->getConnectionUrl($scopes));
    exit;
}

// Unsplash sends user back with ?code=XXX, use this code to generate AccessToken
if (isset($_GET['code']) && !isset($_SESSION['token'])) {
    \Crew\Unsplash\HttpClient::init([
        'applicationId' => 'XXXXX',
        'secret'        => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        'callbackUrl'   => 'http://unsplash-api.dev'
    ]);

    try {
        $token = \Crew\Unsplash\HttpClient::$connection->generateToken($_GET['code']);
    } catch (Exception $e) {
        print("Failed to generate access token: {$e->getMessage()}");
        exit;
    }

    // Store the access token, use this for future requests
    $_SESSION['token'] = $token;
}

// Send requests to Unsplash
\Crew\Unsplash\HttpClient::init([
        'applicationId' => 'XXXXX',
        'secret'        => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    'callbackUrl'   => 'http://unsplash-api.dev'
], [
    'access_token' => $_SESSION['token']->getToken(),
    'expires_in' => 30000,
    'refresh_token' => $_SESSION['token']->getRefreshToken()
]);

$httpClient = new \Crew\Unsplash\HttpClient();
$owner = $httpClient::$connection->getResourceOwner();

print("Hello {$owner->getName()}, you have authenticated successfully");
aaronklaassen commented 6 years ago

It looks like you're using the numeric ID as applicationId. It should be your 64-character access key.

We'll update the docs to make that more clear.

ghost commented 6 years ago

Yeah in the API screen on the website its called Application ID so thats confusing. However when using the access code instead it gives me a new error:

Failed to generate access token: invalid_grant: The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.

And now it looks like this:

        'applicationId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'secret'        => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'callbackUrl'   => '<LINK TO PHP PAGE>',
        'utmSource'     => 'AppName'
aaronklaassen commented 6 years ago

Yeah you're right about the confusion; we'll be fixing that.

This isn't an unsplash-php issue - can you send me some more details? You can get in touch with us at api@unsplash.com.

ghost commented 6 years ago

I think I already solved it.

On https://github.com/unsplash/unsplash-php/blob/master/examples/oauth-flow.php (line 11) it shows a custom link. On line 26 and 44 those are different. So that made me think I only needed to change the first callbackurl. However when changing the others to the same as line 11 it seems to work.

ghost commented 6 years ago

However i'm struggling getting it to work. Would be great if I had some real-world examples. So far I failed with even doing a simple search. Might be mainly because my object-orientated programming skills are limited. So It would help me alot to have some good examples on basic tasks.

$search = 'forest';
$page = 3;
$per_page = 15;
$orientation = 'landscape';

$test = $httpClient::$connection->photos($search, $page, $per_page, $orientation);
echo "results:";
var_export($test);

No luck so far...

aaronklaassen commented 6 years ago

@Xionax OAuth problems are almost always that there's a mismatch in your requests; everything must match: key and secret, redirect URI, requested scopes, etc. I can help you via email.