Question in short form: What is the cleanest way to implement Google Api OAuth2 authentication in Magento for the Admin area
Question in long form: All the new Google APIs are using OAuth2. The php client library is here and it abstracts the OAuth2 handling https://code.google.com/p/google-api-php-client/
The process is simple
The examples in the client librarys are all flat files. So I'm looking for the best way to fit it into a MVC structure... or Magento to be precise.
Lets be specific. Its to retrieve Google Contacts. So far I have:
The problem I have is that I can't do that final redirect. Even though I am using the adminhtml helper getUrl method which does append the 'key' url parameter, when i redirect from frontend to backend I get kicked to the Dashboard.
Is there a better way to implement Googles OAuth2 in Magento?
How do you redirect to a direct Admin URL?
The whole auth action which just renders a login link was pointless.
As soon as the adminhtml_contacts/index action realises that there is no access token it is able to use the Google_Client::createAuthUrl to work out where the user should be sent. So why bother putting this into an actual link? Instead I just immediately redirect them to the authUrl.
If the user is already logged into google then they don't need to do anything. Google sees they are logged in and immediately redirects back to my specified (and predictable) frontend controller action.
This frontend controller action stores the access token and I redirect back to adminhtml_contacts/index action. It still bums out because of the 'key' url parameter cross site request forgery protection problem specified above.
To get round this I turned off the secret key just for this action using a preDispatch hook in the admin controller.
public function preDispatch()
{
if ($this->getRequest()->getActionName() == 'index') Mage::getSingleton('adminhtml/url')->turnOffSecretKey();
parent::preDispatch();
}
Its not ideal but works and means I can actually start work on the api rather than mess around with authentication.
Now to get to grips with the Contacts API which has no client abstraction so I have to wade in deep into SimpleXml namespace issues and cumbersome DOMDocument manipulation. Hey ho.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With