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
- Does user have an AccessToken?
- No
- Okay create a login url using client library
- User clicks on login url link which takes them to Google to login
- Once authenticated Google redirects back to Magento via the redirect url specified
- Google sends back an AccessToken as part of url. Store it.
- Make calls to the various APIs using this AccessToken
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:
- An admin controller called ContactsController with an index action. The very first thing it does is check if there is an access token. If there is no access token it forwards to an auth action.
- The auth action simply renders a block which has the AuthUrl generated by the google php client library "createAuthUrl()"
- On Clicking the link the google login page loads and I login
- Google redirects back to the URL I have specified in the code (and the Google API Console https://code.google.com/apis/console). They also passes back the Access token. This URL must be predictable so it can't be an admin area url as these are dependent on a 'key' url parameter. Therefore I have created a frontend controller and action for Google to redirect back to.
- In the frontend controller I store the AccessToken.
- I then redirect back to the admin controller ContactsController index action. It sees an AccessToken and the application can actually do some stuff.
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?