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?
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.
It is not free.
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.
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'));
Check here for an updated CakePHP 3 tutorial and plugin:
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