Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP OAuth with Google

Tags:

oauth

cakephp

I would like to use Google OAuth in my cakePHP application to let the users login with their google account. I looked at the following component: http://code.42dh.com/oauth/ . Somehow I am not able to get it up and working. I don't know what I am doing wrong. I registered my application on the Google registration form and got my Consumer key and Consumer secret. I added it in the consumer component. I still don't get it to work.

Here is my code:

<?php

class ExampleController extends AppController {
public $uses = array();
var $helpers = array('Javascript', 'Ajax');
public $components = array('OauthConsumer');

public function google() {

    $scope = "https://www.google.com/m8/feeds/";
    $REQUEST_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetRequestToken?scope=' . urlencode($scope);

    $requestToken = $this->OauthConsumer->getRequestToken('Google', $REQUEST_TOKEN_URL, 'http://mydomain.com/example/google_callback');
    $this->Session->write('google_request_token', $requestToken);
    $this->redirect('https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=' . $requestToken->key);
}

public function google_callback() {
    $requestToken = $this->Session->read('google_request_token'); 
    $accessToken = $this->OauthConsumer->getAccessToken('Google',   'https://www.google.com/accounts/OAuthGetAccessToken', $requestToken); 

 }
 }

 ?>

When I try to request the requestToken I get the answes: "signature invalid".

Did someone use Google OAuth within their cakePHP application and would be willing to give me some tips?

like image 749
nino Avatar asked Jan 05 '12 11:01

nino


People also ask

Does Google use OAuth?

Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, client-side, installed, and limited-input device applications. To begin, obtain OAuth 2.0 client credentials from the Google API Console.

Is Google OAuth 2.0 free?

It is not free.

What is redirect URL for OAuth?

The redirect URL is the endpoint for your application or web page that processes the seller authorization response and manages the seller's OAuth tokens. You need to add this URL to your application using the Developer Dashboard.


2 Answers

I think the issue is the querystring in the request token url. Try the following:

$REQUEST_TOKEN_URL = 'https://www.google.com/accounts/OAuthGetRequestToken';
$requestToken = $this->OauthConsumer->getRequestToken('Google', $REQUEST_TOKEN_URL, 'http://mydomain.com/example/google_callback', 'GET', array('scope' => 'https://www.google.com/m8/feeds'));
like image 92
dhofstet Avatar answered Oct 13 '22 12:10

dhofstet


Check here for an updated CakePHP 3 tutorial and plugin:

  • Login with Google Oauth2 in CakePHP 3 using CakeDC/Users Plugin
like image 36
steinkel Avatar answered Oct 13 '22 10:10

steinkel