Complete oAuth script for Twitter and LinkedIn using PECL oAuth Extension

37 thoughts on “Complete oAuth script for Twitter and LinkedIn using PECL oAuth Extension”

  1. Excellent 🙂

    You are indeed a lifesaver, now… all we need is one to work with facebook if applicable and we’re off!

    Thank you 🙂

  2. hey dude thnx for the nice tutorial .

    am really new to this.. .

    can u suggest me how i can get the pecl extension to working , so tha ti can use this code

  3. Fantastic work.
    Do you have one for youtube?
    I started one with PECL oAuth, but cannot get it working. I do get the key though…
    function get_r_token(){
    try {

    /* Google scopes are in the following format: urlencoded(scope) urlencoded(scope) */
    $scopes = urlencode(“”);

    $arrayResp = $o->getRequestToken(“{$scopes}”);
    file_put_contents(OAUTH_TMP_DIR . “/request_token_resp”,serialize($arrayResp));
    $authorizeUrl = “{$arrayResp[“oauth_token”]}”;
    if(PHP_SAPI==”cli”) {
    echo “Navigate your http client to: {$authorizeUrl}n”;
    } else {
    header(“Location: {$authorizeUrl}”);
    } catch(OAuthException $E) {
    echo “Response: “. $E->lastResponse . “n”;

    function get_a_token(){
    try {
    $request_token_info = unserialize(file_get_contents(OAUTH_TMP_DIR . “/request_token_resp”));
    $arrayResp = $o->getAccessToken(“”);
    file_put_contents(OAUTH_TMP_DIR . “/access_token_resp”,serialize($arrayResp));
    echo “Finished getting the access token!n”;
    } catch(OAuthException $E) {
    echo “Response: “. $E->lastResponse . “n”;

    function delete(){
    $request_token_info = unserialize(file_get_contents(OAUTH_TMP_DIR . “/access_token_resp”));
    /* put the OAuth params into the Authorization header */

    $api_args = array(“empty”=>”empty”);
    $oauth->fetch(GOOGLE_YT_EDIT_VIDEO.’OJG_857fsNw’, ”, OAUTH_HTTP_METHOD_DELETE, array(“User-Agent” => “pecl/oauth”));


  4. Hey there- I get this error:

    Fatal error: Class ‘OAuth’ not found in C:wampwwwsite_includestwitter.php on line 14

    Little help?

  5. Huh? I don’t understand this code at all. Where do you define the class OAuth?

    $oauthc = new OAuth($oauth[‘twitter’][‘consumerkey’],

    Are you using a library that you forgot to mention?

  6. Hello,
    I am also getting the error like OAuth class not found in…/linkedin.php. Can you tell me what the corrections I need to do?

  7. I get this exception: exception ‘OAuthException’ with message ‘Invalid auth/bad request (got a 400, expected HTTP/1.1 20X or a redirect)’

    Which raises on this line: $data = $oauthc->fetch(‘’);

    Can anyone help me with this please? Thanks!

    PS. Shouldn’t “$oauth_verifier” be used in somewhere?

    1. I’ve fixed that problem replacing the line by this:

      $data = $oauthc->fetch(‘‘, NULL, OAUTH_HTTP_METHOD_GET);

  8. I tried it and nothing works. All I get is this error:
    PHP Fatal error: Class ‘OAuth’ not found in C:Inetpubwwwrootmarioninteractive.comlinkedinlinkedin.php on line 14

    I checked line 14 and even the other lines and don’t see any issues.


  9. Some notes for people having trouble.

    $oauth = new OAuth

    is a standard PHP package. One that a lot of server don’t have by default. You have to install it or request your hosting company to do so (if they will). It’s not easy, but googling should help find the specifics for your OS.

    Problems with getAccessToken

    I found I had to change to code to the following:

    $access_token_info = $oauth->getAccessToken($accesstokenurl, “”, $token);

    $token is five digit number you get from linkedin.

    Problems with $_REQUEST[‘oauth_token’]

    This should be saved in the beginning like so:

    $_SESSION[‘lrequest_oauth_token’]= $request_token_info[‘oauth_token’];

    and then before setting the token, use :

    $oauth_token = $_SESSION[‘lrequest_oauth_token’];

    use $oauth_token instead of $_REQUEST[‘oauth_token’]

  10. Hey just wanted to give you a quick heads up. The words in your article seem to be running off the screen in
    Firefox. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I figured I’d post to let you know.

    The layout look great though! Hope you get the issue resolved soon.

Leave a Reply

Your email address will not be published. Required fields are marked *