2012-01-05 103 views
13

我想在我的cakePHP应用程序中使用Google OAuth让用户使用其Google帐户登录。我看了下面的组件:http://code.42dh.com/oauth/。不知何故,我无法启动并正常工作。我不知道我做错了什么。我在Google注册表上注册了我的申请,并获得了我的消费者密钥和消费者密钥。我将其添加到消费者组件中。我仍然没有得到它的工作。CakePHP OAuth与谷歌

这里是我的代码:

<?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); 

} 
} 

?> 

当我尝试要求requestToken我得到answes: “签名无效”。

有人在他们的cakePHP应用程序中使用Google OAuth并愿意给我一些提示吗?

回答

6

我认为问题是请求令牌URL中的查询字符串。请尝试以下操作:

$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')); 
0

你忘了让你cliend ID和秘密密钥连接公共API访问键和URL参照网址...

创建一个公共的API访问,并在你的设置中添加它。