Hello,
I’m trying to do a Oauth call to Magento 1.9 with the AuthClient extension.
My code:
try {
$callbackUrl = "*********************/web/cronjob/syncmage";
$temporaryCredentialsRequestUrl = "*************/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = '**************/beheer/oauth_authorize';
$accessTokenRequestUrl = '**************/oauth/token';
$apiUrl = '*********/api/rest';
$consumerKey = '**************';
$consumerSecret = '*****************';
$session = Yii::$app->session;
if (!$session->isActive) {
$session->open();
}
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
}
if (!isset($session['state'])) {
$session['state'] = false;
}
sleep(3);
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new \OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
$oauthClient->disableSSLChecks();
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/customers?page=".$page."&limit=".$limit;
$activeContacts = [];
$activeCompanies = [];
$usersList = $this->call($oauthClient, $resourceUrl);
if($usersList) {
foreach($usersList as $user) {
$userId = $user->entity_id;
array_push($activeContacts, $userId);
$resourceUrl = "$apiUrl/customers/$userId/addresses";
$addressList = $this->call($oauthClient, $resourceUrl);
// CONTACTS
$dbContact = MagContact::find()->where(['mag_id' => $userId])->one();
if(!$dbContact) {
$contact = new MagContact();
// Find Teamleader Id
$tlContact = \app\models\Contact::find()->where(['email' => $user->email])->one();
if($tlContact) {
$contact->tl_id = $tlContact->teamleader_id;
}
} else {
$contact = $dbContact;
}
// COMPANIES
foreach($addressList as $address) {
//code..
}
}
}
}
} catch (\OAuthException $e) {
$this->dump($e->lastResponse);
$this->dump($e->getMessage());
}
This redirects to a Magento page where I have to authenticate myself. Is it possible to do this automatically, so the script can run as cronjob?