Reputation: 1980
I try to use the script php-soundcloud (Oauth 2) and I encounter a problem after user login.
I do exactly the tutorial wiki: https://github.com/mptre/php-soundcloud/wiki/Oauth-2
When the user has accepted the application, it returns the following URL: http://mywebsite.com/?code=123456789123456789
I get the correct $_GET['code']
but the function accessToken()
will not retrieve the information, it gives me a HTTP code 401 error (see the picture below).
Here is my code (the most basic):
<?php
include 'Services/Soundcloud.php';
$soundcloud = new Services_Soundcloud('_myClientId_', '_ClientSecret_', '_RedirectUri_');
echo '<a href="' . $soundcloud->getAuthorizeUrl() . '">Connect with SoundCloud</a><br />';
try {
$accessToken = $soundcloud2->accessToken($_GET['code']);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
Here is HTTP Status Codes of soundcloud: http://developers.soundcloud.com/docs/api/guide#errors
They say that the code 401 is: Make sure you're sending us a client_id
or access_token
.
But I'm sure to send the correct id and token! :/
If someone has already manipulate SoundCloud API and know why I get this error, let me know of any solution.
Upvotes: 1
Views: 2712
Reputation: 3067
atmon3r's answer is certainly wrong. The call to setAccessToken() is completely unnecessary, as the call to accessToken() already sets the access token. Also, he was getting an exception when calling accessToken(), so it's impossible that adding a line of code after the one that issued the exception could fix it.
I had had the same error, and in my case it was caused by calling the Services_Soundcloud constructor without the third (redirect_uri) parameter. Adding this parameter (which shouldn't be necessary in the first place as it is completely useless, but the SoundCloud API is terribly designed) made it work for me (with no need to call setAccessToken).
Besides the API being ill-conceived (in that it only allows for one fixed return url configured in the application [already a stupid and crippling limitation by itself] and at the same time requires it as a parameter [when it can only have one valid value]), the PHP library is flawed, in that it won't throw an exception if you call the constructor without the third parameter which is required.
Note that the value of return_url must match the one configured in the application.
Upvotes: 0
Reputation: 1980
ok, i have find! it's so easy...
I had not seen the function setAccessToken()
which adds the token to the class
try {
$accessToken = $soundcloud2->accessToken($_GET['code']);
// this is the part that I was missing
$soundcloud2->setAccessToken($accessToken["access_token"]);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
Upvotes: 1