1

Hi I want to integrate the Yahoo BOSS API with Symfony2, but the OAuth code class suggested by Yahoo doesn’t seem to work with modern PHP frameworks.

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

I think the OAuth class has namespace issues, what steps do I need to take to implement this class in Symfony2?

3
  • 1
    You could create it as a service - see symfony.com/doc/current/book/service_container.html Commented Sep 16, 2013 at 14:54
  • Hi I intend to implement it as a service once I have it working. Thanks! Commented Oct 19, 2013 at 12:04
  • did you find my answer useful? Commented Oct 24, 2013 at 8:47

2 Answers 2

5
+200

1) Create a directory like Project/src/OAuth

2) Place the classes in separate files inside that directory.

3) Add namespace OAuth; at the beginning of every OAuth class.

4) Add a backslash to the Exception class (or add use Exception;):

class OAuthException extends \Exception

5) Avoid underscores in class names (Laravel-Oauth2 Issue in Laravel 4 , Underscores in Namespaces and Class Names):

abstract class OAuthSignatureMethodRSASHA1 extends OAuthSignatureMethod

class OAuthSignatureMethodPLAINTEXT extends OAuthSignatureMethod

class OAuthSignatureMethodHMACSHA1 extends OAuthSignatureMethod

6) Fix the array_map call in OAuthUtil:

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

7) And finally use it:

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

So those are the steps I followed and it worked; you can grab the classes from here: https://github.com/coma/OAuthSOSample

Sign up to request clarification or add additional context in comments.

Comments

4

In Symfony 2 I can recommend a third party bundle that will support OAuth 1 & 2.

Take at look: https://github.com/hwi/HWIOAuthBundle

2 Comments

Hi I shouldn't need to add a bundle just to access a REST API with OAuth 1, that project has a ton of functionality that I don't need. Thanks though!
No one will force you to add this bundle. But you can take a look how it works and take what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.