【问题标题】:Use OAuth 1 Class in Symfony2在 Symfony2 中使用 OAuth 1 类
【发布时间】:2013-10-22 06:23:22
【问题描述】:

您好,我想将 Yahoo BOSS API 与 Symfony2 集成,但 Yahoo 建议的 OAuth 代码类似乎不适用于现代 PHP 框架。

http://oauth.googlecode.com/svn/code/php/OAuth.php

/* Generic exception class
 */
class OAuthException extends Exception {
  // pass
}

class OAuthConsumer {
  public $key;
  public $secret;

  function __construct($key, $secret, $callback_url=NULL) {
    $this->key = $key;
    $this->secret = $secret;
    $this->callback_url = $callback_url;
  }

  function __toString() {
    return "OAuthConsumer[key=$this->key,secret=$this->secret]";
  }
} [...]

http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#oauth_php

我认为 OAuth 类存在命名空间问题,我需要采取哪些步骤在 Symfony2 中实现该类?

【问题讨论】:

标签: php symfony oauth


【解决方案1】:

1) 创建一个目录,如 Project/src/OAuth

2) 将类放在该目录内的单独文件中。

3) 在每个 OAuth 类的开头添加namespace OAuth;

4) 在 Exception 类中添加反斜杠(或添加use Exception;):

class OAuthException extends \Exception

5) 避免在类名中使用下划线(Laravel-Oauth2 Issue in Laravel 4Underscores in Namespaces and Class Names):

abstract class OAuthSignatureMethodRSASHA1 extends OAuthSignatureMethod

class OAuthSignatureMethodPLAINTEXT extends OAuthSignatureMethod

class OAuthSignatureMethodHMACSHA1 extends OAuthSignatureMethod

6) 修复 OAuthUtil 中的 array_map 调用:

return array_map(array('OAuth\OAuthUtil', 'urlencode_rfc3986'), $input);

7) 最后使用它:

<?php

namespace My\PlaygroundBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use OAuth\OAuthConsumer;
use OAuth\OAuthRequest;
use OAuth\OAuthSignatureMethodHMACSHA1;
use OAuth\OAuthUtil;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     * @Template()
     */
    public function indexAction()
    {
        $cc_key  = "your consumer key here";
        $cc_secret = "your consumer secret here";
        $url = "http://yboss.yahooapis.com/ysearch/news,web,images";
        $args = array();
        $args["q"] = "yahoo";
        $args["format"] = "json";

        $consumer = new OAuthConsumer($cc_key, $cc_secret);
        $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);
        $request->sign_request(new OAuthSignatureMethodHMACSHA1(), $consumer, NULL);
        $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
        $ch = curl_init();
        $headers = array($request->to_header());
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $rsp = curl_exec($ch);
        $results = json_decode($rsp);

        return array(
            'results' => $results
        );
    }
}

所以这些是我遵循的步骤并且有效;你可以从这里获取课程:https://github.com/coma/OAuthSOSample

【讨论】:

    【解决方案2】:

    在 Symfony 2 中,我可以推荐支持 OAuth 1 和 2 的第三方包。

    看一看:https://github.com/hwi/HWIOAuthBundle

    【讨论】:

    • 您好,我不需要添加捆绑包来访问具有 OAuth 1 的 REST API,该项目有大量我不需要的功能。不过谢谢!
    • 没有人会强迫你添加这个捆绑包。但是你可以看看它是如何工作的,并采取你所需要的。
    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 2017-06-20
    • 2014-04-18
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多