1. 程式人生 > >獲取後臺用戶 token 的方法

獲取後臺用戶 token 的方法

cto agent efault protected user token elf ted pri

一,通過電子郵箱獲取用戶token

protected function getCustomerToken($emailId){
 /**
* @var \Magento\Customer\Model\Customer $customer */
*/
$customer->loadByEmail($emailId);
if($customer->getId()){
        /**
        * @var \Magento\Integration\Model\Oauth\TokenFactory $tokenModelFactory 
        */
        $customerToken = $this->tokenModelFactory->create();
        $tokenKey = $customerToken->createCustomerToken($customerId)->getToken();
        return $tokenKey;
}
return "YOU MSG FOR CUSTOMER NOT FOUND";
}


二、通過用戶名和密碼獲取token
public function getToken($username,$password)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$token = $objectManager->get(‘\Magento\Integration\Model\AdminTokenService‘)->createAdminAccessToken($username,$password);
return $token;
}


獲取後臺用戶 token 的方法