Sound Cloud Error - php

Here is my redirect.php, i always get
'HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request. '
php code:
require_once 'Services/Soundcloud.php';
// create client object with app credentials
$client = new Services_Soundcloud('xxxxxxxxxxxxxxxxxxxxxxx');
$client->setAccessToken($_GET['code']);
// make an authenticated call
$current_user = json_decode($client->get('me'));
print $current_user->username;
Any idea?
thanks

You need to set clientID, clientSecret and redirectURI arguments when initializing new Services_Soundcloud.
Your code should look like this:
index.php
<?php
// create client object with app credentials
include('Services/Soundcloud.php');
$client = new Services_Soundcloud('....', '....', 'http://my.redirect.com/url.php');
$authorizeUrl = $client->getAuthorizeUrl();
?>
Connect with SoundCloud
And the redirect url:
<?php
require_once('Services/Soundcloud.php');
// create client object with app credentials
$client = new Services_Soundcloud('.....', '.....', 'http://my.redirect.com/url.php');
try {
$accessToken = $client->accessToken($_GET['code']);
try {
$me = json_decode($client->get('me'), true);
var_dump($me);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
var_dump($e->getMessage());
exit();
}
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
var_dump($e->getMessage());
exit();
}
?>
You can get the clientId and clientSecret from the developers page
Also you can checkout the documentation for proper examples in PHP, Python, Ruby and JS
You can check the code above on this link:
soundcloud.itnews-bg.com/

Related

Unable to decode credential from JWT

I am trying to implement the new "Sign in with Google" button as described in https://developers.google.com/identity/gsi/web/guides/display-button.
Everything is fine, and I am able to get a response from the button with "credential" and "g_csrf_token" elements, which I can send to my server. However, using the Google API Client to decode the credential doesn't work. I'm trying to follow the instructions.
Here's my code:
$id_token = filter_input(INPUT_POST, 'credential');
$csrfToken = filter_input(INPUT_POST, 'g_csrf_token'); //??? Do we need this?
$client = new Google_Client(['client_id' => $clientid]);
$client->addScope("email"); // Recommended in another StackOverflow answer but makes no difference
try {
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload
The result is the error message id_token must be passed in or set as part of setAccessToken.
I've updated my Google API Client to v2.11.
I assume that I've missed a step somewhere - can someone help?
Have found a solution, by trial and error! Turns out that $id_token needs to be passed to the client twice, once in setAccessToken() and then again in verifyIdToken(). Omitting setAccessToken fails (like the error message says), but if you pass it in setAccessToken but NOT in verifyIdToken, that doesn't work either.
$id_token = filter_input(INPUT_POST, 'credential');
$client = new Google_Client(['client_id' => $clientid]);
try {
$client->setAccessToken($id_token);
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload
It would nice, if you're at Google and picking this up, if you updated the documentation.

Fi-ware IDM - Oauth2 php client configuration

I'm trying to use the FiWare Identity Management - KeyRock that provides a Oauth 2.0 login. I have configured my app in the Fiware web page to set the url and callback url and I have got my client ID and my password.
Now I'm trying to use the API with a simple PHP client Oauth2.0 library. I've choosen this. It looks very easy to use, but I have a problem:
When I open my web, I'm correctly redirected to the fi-ware login web page, but once i logged, I'm not redirected to my web page callback page, I continue in the fi-ware labs web page.
That's my code:
index.php:
<?php
require_once 'vendor/autoload.php';
use fkooman\OAuth\Client\Guzzle6Client;
use fkooman\OAuth\Client\ClientConfig;
use fkooman\OAuth\Client\SessionStorage;
use fkooman\OAuth\Client\Api;
use fkooman\OAuth\Client\Context;
$clientConfig = new ClientConfig(
array(
'authorize_endpoint' => 'https://account.lab.fi-ware.org',
'client_id' => 'my_client_id',
'client_secret' => 'my_secret',
'token_endpoint' => 'http://estebanxabi.miwp.eu/otros/callback.php',
)
);
$tokenStorage = new SessionStorage();
$httpClient = new Guzzle6Client();
$api = new Api('foo', $clientConfig, $tokenStorage, $httpClient);
$context = new Context('sampleEmail', array('authorizations'));
$accessToken = $api->getAccessToken($context);
if (false === $accessToken) {
/* no valid access token available, go to authorization server */
header('HTTP/1.1 302 Found');
header('Location: '.$api->getAuthorizeUri($context));
exit;
}
echo 'Access Token: '.$accessToken->getAccessToken();
and callback.php:
<?php
require_once 'vendor/autoload.php';
use fkooman\OAuth\Client\Guzzle6Client;
use fkooman\OAuth\Client\ClientConfig;
use fkooman\OAuth\Client\SessionStorage;
use fkooman\OAuth\Client\Callback;
$clientConfig = new ClientConfig(
array(
'authorize_endpoint' => 'https://account.lab.fi-ware.org',
'client_id' => 'client_ide',
'client_secret' => 'seceret',
'token_endpoint' => 'http://estebanxabi.miwp.eu/otros/callback.php',
)
);
try {
$tokenStorage = new SessionStorage();
$httpClient = new Guzzle6Client();
$cb = new Callback('foo', $clientConfig, $tokenStorage, $httpClient);
$cb->handleCallback($_GET);
header('HTTP/1.1 302 Found');
header('Location: http://localhost/fkooman/php-oauth-client/example/simple6/index.php');
exit;
} catch (fkooman\OAuth\Client\Exception\AuthorizeException $e) {
// this exception is thrown by Callback when the OAuth server returns a
// specific error message for the client, e.g.: the user did not authorize
// the request
die(sprintf('ERROR: %s, DESCRIPTION: %s', $e->getMessage(), $e->getDescription()));
} catch (Exception $e) {
// other error, these should never occur in the normal flow
die(sprintf('ERROR: %s', $e->getMessage()));
}
I've never use that library, but taking a look... are you sure "token_endpoint" is correctly configured? It is not the same the token endpoint (/oauth2/token) than the callback URL.
BR

Copy google doc spreadsheet using php api

I want to copy Google Doc spreadsheet file.This code I am using
function copyFile($service, $originFileId, $copyTitle) {
$copiedFile = new Google_DriveFile();
$copiedFile->setTitle($copyTitle);
try {
return $service->files->copy($originFileId, $copiedFile);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('myclientid');
$client->setClientSecret('myclient secret');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
copyFile($service,'my schema id','Copy Of Schema');
I am not able to get $service instance. SO I searched and get the above way to do but now it is giving 401 login required error.
Please help me
You need to check for sufficient authorization from the user
you need to generate authorization URL:
$authUrl = $client->createAuthUrl();
Redirect user to the authUrl
Make user paste the authorization code:
$authCode = trim(fgets(STDIN));
Authenticate
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
Once above it done, you have now authenticated the user with OAuth to access drive.
Reference: Click here

Can't get soundcloud access_token using php sdk. invalid_grant error

I am using soundcloud php sdk. It works successfully when i use the sdk to get track. But when i've tried to get an access Token in order to use it, the sdk send me back an error 401. After debugging the error message, i ve get the responseJSON below: {"error":"invalid_grant"}
This is my code , i m using Zend framework
$code = $this->getRequest()->getParam('code',false);
if($code){
$client = new Services_Soundcloud($this->soundcloud['client_id'],$this->soundcloud['secret_key']);
try {
$access_token = $client->accessToken($code);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
}
Did anyone has any idea about how to solve this error ?
In order to exchange a code for an access token, you must instantiate your Services_Soundcloud instance with a client id, client secret and a redirect uri. The following code should work, assuming you are storing the redirect uri in $this->soundcloud:
$client = new Services_Soundcloud(
$this->soundcloud['client_id'],
$this->soundcloud['secret_key'],
$this->soundcloud['redirect_uri']
);
$code = $this->getRequest()->getParam('code',false);
if ($code) {
try {
$access_token = $client->accessToken($code);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
}
Let me know if that solves the problem.

Creating new list on Etsy

I am trying to create a new listing on Etsy.
I used oauth to authenticate and got
OAUTH_CONSUMER_KEY
and
OAUTH_CONSUMER_SECRET
I check it with this code and I got return og all the seller data, so everything is ok with the OAuth.
$oauth = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken("key","secret");
try {
$data = $oauth->fetch("http://openapi.etsy.com/v2/users/__SELF__", null, OAUTH_HTTP_METHOD_GET);
$json = $oauth->getLastResponse();
print_r(json_decode($json, true));
} catch (OAuthException $e) {
error_log($e->getMessage());
error_log(print_r($oauth->getLastResponse(), true));
error_log(print_r($oauth->getLastResponseInfo(), true));
exit;
}
I am trying to crate a new listings. First i managed to create a new listing through the api browser on the production. Now, i want to create a new listing through PHP. This is what i did, and it return my error:
This is my code:
$oauth = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken("key","secret");
try {
$url = "http://openapi.etsy.com/v2/listings";
$params = array('description' => 'thisisdesc','materials' => 'yes','price'=>"5.99"
,'quantity'=>"2",'shipping_template_id'=>"52299",'shop_section_id'=>"1"
,'title'=>"thisistitle",'category_id'=>"i_did",'who_made'=>"5.99"
,'is_supply'=>"1",'when_made'=>"2010_2012");
$oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST);
print_r(json_decode($json, true));
} catch (OAuthException $e) {
print_r($e);
error_log($e->getMessage());
error_log(print_r($oauth->getLastResponse(), true));
error_log(print_r($oauth->getLastResponseInfo(), true));
exit;
}
I get the response of:
Invalid auth/bad request (got a 403, expected HTTP/1.1 20X or a redirect)
This method not accessible to restricted API key.
If you are still developing your application you can create listings without gaining full API access, so long as the shop is on your account. Make sure you parse in listings_w as a permission scope when making the first OAUTH request. I have altered the example provided by ETSY in the code below.
// instantiate the OAuth object
// OAUTH_CONSUMER_KEY and OAUTH_CONSUMER_SECRET are constants holding your key and secret
// and are always used when instantiating the OAuth object
$oauth = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
// make an API request for your temporary credentials
$req_token = $oauth->getRequestToken("https://openapi.etsy.com/v2/oaut/request_token?scope=email_r%20listings_r%20listings_w", 'oob');
print $req_token['login_url']."\n";`
Notice the scope=email_r%20listings_r%20listings_w;
Hope this helps
Aha, here's the answer. From an Etsy developer:
Your API was not yet approved for full API access. I've fixed that, so you should be able to use those methods now.
Hence, get in touch with the firm, and ask for your key to be approved.

Categories