Yii Framework Forum: AuthClient authentication without browser/middleman - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

AuthClient authentication without browser/middleman Trying to do an api-call via Oauth 1 with cronjobs. Rate Topic: -----

#1 User is offline   Bart R 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 12-September 17

Posted 12 September 2017 - 03:04 AM

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?
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,145
  • Joined: 17-January 09
  • Location:Russia

Posted 12 September 2017 - 08:17 AM

Not if it's not supported by Magento instance you're authenticating against.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users