获取用户';来自Magento REST API会话的s id


Get user's id from a Magento REST API session?

这是关于这篇文章的后续问题:如何获得用户';来自Magento REST API令牌的id?

我应该为$tokenId变量分配什么?

我尝试了Consumer Key和Access Token,但似乎没有得到用户ID

我不确定我是否完全理解这个问题。但是,如果您试图在API请求期间获取API用户的ID,您将需要通过API会话,如下所示:

$apiUserId = Mage::getSingleton('api/session')->getUser()->getId();

请确保使用getSingleton而不是getModel。否则,这应该在大多数地方都有效,包括活动观察员。

通过我们拥有的唯一accessToken获取令牌的集合。我们将得到的集合只包含一行,因为accessToken是唯一的。使用以下代码进行相同操作:

$oauthCollection = Mage::getModel('oauth/token')->getCollection()->addFieldToFilter('token',$accessToken)->getFirstItem();
// $accessToken is the access token generated

该行将依次保存为其生成令牌的用户Id,该Id可以像一样获取

echo $oauthCollection->getCustomerId(); //For customer type account
echo $oauthCollection->getAdminId(); //For Admin type account
//Only one of the above will hold the ID