Created
June 4, 2015 00:19
-
-
Save dfrankland/e5958947d2d5cdaab33b to your computer and use it in GitHub Desktop.
Examples of how to use Magento OAuth that actually work!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; // This scripts url | |
$magentoUrl = 'http://example.com/'; // The url of your magento installation | |
$temporaryCredentialsRequestUrl = $magentoUrl . 'oauth/initiate?oauth_callback=' . urlencode($callbackUrl); | |
$adminAuthorizationUrl = $magentoUrl . 'admin/oauth_authorize'; | |
$accessTokenRequestUrl = $magentoUrl . 'oauth/token'; | |
$apiUrl = $magentoUrl . 'api/rest'; | |
$consumerKey = 'OAUTH CONSUMER KEY'; // Get from System > Web Services > REST - OAUTH Consumers > Edit Consumer > Key | |
$consumerSecret = 'OAUTH CONSUMER SECRET'; // Get from System > Web Services > REST - OAUTH Consumers > Edit Consumer > Secret | |
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 | |
) | |
); | |
$oauthClient->disableRedirects(); | |
$headers = array('Content-Type' => 'application/json', 'Content_Type' => 'application/json', 'Accept' => '*/*'); | |
$oauthClient->fetch($resourceUrl, $productData, 'POST', $headers); | |
$response = json_decode($oauthClient->getLastResponse()); | |
print_r($response); | |
} | |
} catch (OAuthException $e) { | |
print_r($e->getMessage()); | |
echo "<br>"; | |
print_r($e->lastResponse); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$callbackUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; // This scripts url | |
$magentoUrl = 'http://example.com/'; // The url of your magento installation | |
$temporaryCredentialsRequestUrl = $magentoUrl . 'oauth/initiate?oauth_callback=' . urlencode($callbackUrl); | |
$adminAuthorizationUrl = $magentoUrl . 'admin/oauth_authorize'; | |
$accessTokenRequestUrl = $magentoUrl . 'oauth/token'; | |
$apiUrl = $magentoUrl . 'api/rest'; | |
$consumerKey = 'OAUTH CONSUMER KEY'; // Get from System > Web Services > REST - OAUTH Consumers > Edit Consumer > Key | |
$consumerSecret = 'OAUTH CONSUMER SECRET'; // Get from System > Web Services > REST - OAUTH Consumers > Edit Consumer > Secret | |
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"; | |
$oauthClient->disableRedirects(); | |
$headers = array('Content-Type' => 'application/json', 'Content_Type' => 'application/json', 'Accept' => '*/*'); | |
$oauthClient->fetch($resourceUrl, array(), 'GET', $headers); | |
$response = json_decode($oauthClient->getLastResponse()); | |
print_r($response); | |
} | |
} catch (OAuthException $e) { | |
print_r($e->getMessage()); | |
echo "<br>"; | |
print_r($e->lastResponse); | |
} |
pult
commented
Nov 5, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment