3

Does anyone have or know of working PHP code to complete an oAuth API integration request flow? I've gotten past the activation step, and my application has the following information

    [oauth_consumer_key] => x...0
    [oauth_consumer_secret] => j...l
    [store_base_url] => http://magento.example.com/
    [oauth_consumer_secret] => 5...n

According to the docs, I'm supposed to use this information to make an oAuth request to

/oauth/token/request

for a temporary request token, and then use that temporary request token to fetch a longer term access token from

/oauth/token/access

and this longer term access token can be used to make API requests.

Or -- so I assume. The docs are a bit cryptic on this.

For example, this section makes mention of the parameters to use for the request to /oauth/token/request, but doesn't provide what the oAuth version should be, nor how the oauth_signature parameter should be generated. It's also a little confusing when talking about the values you need to send once you have your tokens -- as I'd expect to see a secret in there.

Finally, the checklogin.php code from dev docs makes use of two classes, OAuth\Common\Consumer\Credentials and OauthClient.

$credentials = new \OAuth\Common\Consumer\Credentials($consumerKey, $consumerSecret, $magentoBaseUrl);
$oAuthClient = new OauthClient($credentials);

The first seems to be part of the lusitanian/oauth composer package, which is easy enough to use. However -- the OauthClient seems to be part of Magento's API testing framework, which makes it not trivial or obvious how to use it.

I know I can also use my admin username or customer session as API credentials. I'm specifically interested in API Integrations.

  • Not an answer, but one thing that can get in the way here is Magento (thanks to a bug) only gives you once chance to capture your access tokens unless you clear them from the oauth_token table github.com/magento/magento2/issues/2540 – Alan Storm Feb 16 '17 at 7:03
  • I followed Magento Doc and created checklogin.php, endpoint.php, login.php and OauthClient.php. When I try to activate API Integration, It is giving error: Fatal error: Uncaught Error: Class 'OauthClient' not found in /public_html/magento2/checklogin.php:22 Stack trace: #0 {main} thrown in /public_html/magento2/checklogin.php on line 22 Where should I place the OauthClient.php file? – ram666 Mar 20 '18 at 13:07
2

We implement the oAuth request token from openAM and created the new user in Magento2. As we manage user groups and role in our system and using oAuth token to validate it. Hopefully this will help you little

// get admin Token to create the customer Account in realm Data stores
        $ch = curl_init('http://openam/json/authenticate');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'X-OpenAM-Username: uradmin',
            'X-OpenAM-Password: urpassword',
            'Accept-API-Version: resource=2.0, protocol=1.0'
          )
        );

        $result = curl_exec($ch);

        curl_close($ch);

        if (array_key_exists('tokenId', json_decode($result, true))) {
            $adminToke = json_decode($result, true)["tokenId"];

            //create user in OpenAM Customer Realm
            $data = array("username" => $email, "userpassword" => $password,"mail"=>$email);
            $data_string = json_encode($data);

            $ch = curl_init('http://openam/json/admingroup/users/?_action=create');
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'iplanetDirectoryPro:'.$adminToke.''
              )
            );


            curl_close($ch);

            // create Magento Customer in Magento DB


            $customer=$this->_objectManager->create('Magento\Customer\Model\Customer');
            $customer->setWebsiteId($this->_objectManager->create('Supplier\Delegate\Helper\Data')->getCurrentWebsiteId());
            $customer->setStoreId($this->_objectManager->create('Manufacture\Dealers\Helper\Data')->getCurrentStoreId());
            $customer->setGroupId(5);
            $customer->setFirstname($firstName);
            $customer->setLastname($lastName);
            $customer->setEmail($email);
            $customer->setPassword($password);


            try{
                $customer->save();
            }
            catch   (\Exception $exception)
            {
                $this->messageManager->addError($exception->getMessage());
                return $this->resultRedirectFactory->create()->setPath('*/*/register');
            }
            //save SupplierDelgate Model
            $model = $this->_objectManager->create('Manufacture\Dealers\Model\Suppliers');
            $model->setDelegateId($customer->getId());
            $model->setParentSupplierId($supplierParentId);
            $model->setRole($role);
            $model->save();

            $address =$this->_objectManager->create('Magento\Customer\Model\Address');
            $address->setCustomerId($customer->getId())
              ->setFirstname($customer->getFirstname())
              ->setMiddleName($customer->getMiddlename())
              ->setLastname($customer->getLastname())
              ->setCountryId('SA')
              //->setRegionId('1') //state/province, only needed if the country is USA
              ->setPostcode($postCode)
              ->setCity($city)
              ->setTelephone($telephone)
              ->setFax($fax)
              ->setCompany($company)
              ->setStreet($street)
              ->setIsDefaultBilling('1')
              ->setIsDefaultShipping('1')
              ->setSaveInAddressBook('1');

            try{
                $address->save();
                $this->_objectManager->create('Manufacture\Dealers\Helper\Data')->setCustomerStatus("Active",$customer->getId());

                $this->messageManager->addSuccess(__('registration have been completed Successfully '));
                $this->_redirect('customer/account/login/');

            }
            catch (Exception $e) {
                Zend_Debug::dump($e->getMessage());
            }

--Yogi--

  • 1
    I'm not sure what question this is answering, but it's not what I'm looking for. I want to authenticate with the oAuth API that ships with Magento . This seems to authenticate with some other, third party oAuth based API. – Alan Storm Feb 16 '17 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.