从Magento REST API会话中获取用户的ID?

时间:2015-03-26 11:55:58

标签: php rest magento

这是关于这篇文章的后续问题: How do you get a user's id from a Magento REST API token?

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

我尝试了使用者密钥以及访问令牌,但我似乎没有获得用户ID

2 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解这个问题。但是,如果您在API请求期间尝试获取 API用户的 ID,则需要执行以下API会话:

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

请务必使用getSingleton而非getModel。否则,这应该适用于大多数地方,包括事件观察员。

答案 1 :(得分:0)

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

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

该行依次保存生成令牌的用户ID,可以抓取,如:

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