【问题标题】:Zend Framework 2 ZendOAuth with GoogleZend Framework 2 ZendOAuth 与 Google
【发布时间】:2014-01-02 18:54:36
【问题描述】:

我对这里的 ZendFramework/ZendOAuth 库比较熟悉 https://github.com/zendframework/ZendOAuth

我可以在 Twitter 上使用它,但我不知道如何让它与 Google OAuth 2.0 一起使用。我尝试使用的代码如下:

$config = array(
        'callbackUrl' => 'https://www.example.com/callback',
        'siteUrl' => 'https://accounts.google.com/o/oauth2',
        'userAuthorizationUrl' => 'https://accounts.google.com/o/oauth2/auth',
        'requestTokenUrl' => 'https://accounts.google.com/o/oauth2/token',
        'consumerKey' => 'MY-CONSUMER-KEY-HERE',
        'consumerSecret' => 'MY-SECRET-KEY-HERE',
        'version' => '2.0',
    );

    $scopes = array(
        'https://www.googleapis.com/oauth2/v2/userinfo'
    );

    $consumer = new Consumer($config);
    $token = $consumer->getRequestToken(array('scope' => implode('', $scopes), 'response_type' => 'code', 'redirect_uri' => 'https://www.example.com/callback'));
    $consumer->redirect();

根据 ZendOAuth 的文档,getRequestToken 函数中的数组被视为自定义 OAuth 参数。但是,当我运行此代码时,我会被重定向到显示以下内容的 Google 页面:

缺少必需参数:response_type

浏览器中的网址显示如下:

https://accounts.google.com/o/oauth2/auth?oauth_token=&oauth_callback=https%3A%2F%2Fwww.example.com%2Fcallback

我尝试在 ZendOAuth 库中抛出调试语句,但我似乎无法找到问题所在。

如果有人对此有解决方案,将不胜感激。

谢谢,

【问题讨论】:

    标签: php oauth zend-framework2 oauth-2.0 google-oauth


    【解决方案1】:

    我相信 ZendOAuth 包只支持 OAuth 1,所以对于 OAuth 2,你需要找到一个不同的库。我已将 https://github.com/php-loep/oauth2-client 与 ZF2 应用程序一起使用,因此您可以从那里开始。

    【讨论】:

    • 嗨,蒂姆,我回去搜索并找不到 ZendOAuth 支持的版本,所以我想说你可能是对的,ZendOAuth 2.* 不支持 OAuth2。不过给你一个问题。使用 Http 客户端时,所有这些库如何启动重定向?每当我尝试使用 Http 客户端时,ZF2 不会将我重定向到 Google 页面,而是将其嵌入到我的页面中(网址不会改变),这是不正确的。我似乎无法弄清楚所有这些库是如何通过 Http 客户端直接实现的。有什么想法吗?
    • 这些库通常会使用 PHP header() 函数进行重定向。在您问题的代码示例中,当您调用$consumer->redirect() 时会发生这种情况,请参阅github.com/zendframework/ZendOAuth/blob/master/library/…
    • 谢谢,我现在知道了,并决定创建自己的库。我敢肯定还有其他人有我的烦恼。 code.nraboy.com/zfgoogle
    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多