2

I'm looking for a working example of a PHP script for authentication using OAuth.

I have followed the testing instructions here (http://www.magentocommerce.com/api/rest/testing_rest_resources.html) using the REST client on Firefox.

I already have created a consumer so I have the Consumer Key and Consumer Secret. But, according to the website I also need an Access Token and an Access Token Secret. I'm not entirely sure how to get those. I've googled other examples for different APIs (e.g., Twitter), but I cannot port the knowledge over to Magento.

Any help is appreciated.

asked Jun 30, 2014 at 18:30

2 Answers 2

4

The documentation on Magento's site has a good example here

Here's one of the examples that they give for creating a simple product as an Admin user with OAuth authentication.

session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
 $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
 $_SESSION['secret'] = $requestToken['oauth_token_secret'];
 $_SESSION['state'] = 1;
 header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
 exit;
} else if ($_SESSION['state'] == 1) {
 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
 $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
 $_SESSION['state'] = 2;
 $_SESSION['token'] = $accessToken['oauth_token'];
 $_SESSION['secret'] = $accessToken['oauth_token_secret'];
 header('Location: ' . $callbackUrl);
 exit;
} else {
 $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
 $resourceUrl = "$apiUrl/products";
 $productData = json_encode(array(
 'type_id' => 'simple',
 'attribute_set_id' => 4,
 'sku' => 'simple' . uniqid(),
 'weight' => 1,
 'status' => 1,
 'visibility' => 4,
 'name' => 'Simple Product',
 'description' => 'Simple Description',
 'short_description' => 'Simple Short Description',
 'price' => 99.95,
 'tax_class_id' => 0,
 ));
 $headers = array('Content-Type' => 'application/json');
 $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
 print_r($oauthClient->getLastResponseInfo());
}
} catch (OAuthException $e) {
print_r($e);
}
answered Jan 15, 2015 at 19:13
0

PHP Script to demonstrate Oauth call with signature generation process. refer stackexchange link

answered Aug 11, 2022 at 1:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.