获取后台用户 token 的方法

一,通过电子邮箱获取用户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;
}


posted @ 2017-09-15 10:47  神龙诀  阅读(4368)  评论(0编辑  收藏  举报