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 {
    $o = new OAuth(GOOGLE_OAUTH_CONSUMER_KEY,GOOGLE_OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);

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

    $arrayResp = $o->getRequestToken(“https://www.google.com/accounts/OAuthGetRequestToken?scope={$scopes}”);
    file_put_contents(OAUTH_TMP_DIR . “/request_token_resp”,serialize($arrayResp));
    $authorizeUrl = “https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token={$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 {
    $o = new OAuth(GOOGLE_OAUTH_CONSUMER_KEY,GOOGLE_OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
    $request_token_info = unserialize(file_get_contents(OAUTH_TMP_DIR . “/request_token_resp”));
    $o->setToken($request_token_info[“oauth_token”],$request_token_info[“oauth_token_secret”]);
    $arrayResp = $o->getAccessToken(“https://www.google.com/accounts/OAuthGetAccessToken”);
    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(){
    $oauth = new OAuth(GOOGLE_OAUTH_CONSUMER_KEY,GOOGLE_OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
    $request_token_info = unserialize(file_get_contents(OAUTH_TMP_DIR . “/access_token_resp”));
    $oauth->setNonce(rand());
    $oauth->setToken($request_token_info[“oauth_token”],$request_token_info[“oauth_token_secret”]);
    print_r($request_token_info);
    /* put the OAuth params into the Authorization header */
    $oauth->setAuthType(OAUTH_AUTH_TYPE_AUTHORIZATION);

    $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:\wamp\www\site_includes\twitter.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(‘http://api.linkedin.com/v1/people/~’);

    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(β€˜http://api.linkedin.com/v1/people/~β€˜, 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:\Inetpub\wwwroot\marioninteractive.com\linkedin\linkedin.php on line 14

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

    Anyone?

  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.
    Thanks

Leave a comment