Nautilus
Nautilus

Reputation: 115

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

This is a followup question regarding this post: How do you get a user's id from a Magento REST API token?

What should I assign to the $tokenId variable?

I tried the Consumer Key as well as the Access Token, but I do not seem to get the user ID

Upvotes: 1

Views: 1780

Answers (2)

Tyler V.
Tyler V.

Reputation: 2561

I'm not sure that I fully understand the question. But if you're trying to get the API User's ID during an API request, you'll want to go through the API session like so:

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

Make sure to use getSingleton and not getModel. Otherwise, this should work from most any place, including event observers.

Upvotes: 1

Pratyoosh Mahajan
Pratyoosh Mahajan

Reputation: 13

Fetch the token's collection by the unique accessToken we have. The collection we'll get contains only one row as the accessToken is unique. Use the following code for the same:

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

The row in turn will hold the user Id for whom the token was generated which can be grabbed like:

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

Upvotes: 0

Related Questions