How to get user Access Token and Access Secret with the Twitter API using php

前端 未结 1 1907
名媛妹妹
名媛妹妹 2021-01-19 03:26

I’m trying to code a couple of php pages for getting users tokens the Twitter API 1.1. I’m using the TwitterOAuth library https://twitteroauth.com/

First page : twi

1条回答
  •  [愿得一人]
    2021-01-19 04:29

    Alright, actually managed to figure it out myself. Here is my code for those who need it:

    First page : twitter-go.php

    The user opens it and gets redirected to twitter.com for authorizing the app.

    oauth("oauth/request_token", array("oauth_callback" => "http://boulangerie-colas.fr/twitter/twitter-back.php"));
    //callback is set to where the rest of the script is
    
    //TAKING THE OAUTH TOKEN AND THE TOKEN SECRET AND PUTTING THEM IN COOKIES (NEEDED IN THE NEXT SCRIPT)
    $oauth_token=$request_token['oauth_token'];
    $token_secret=$request_token['oauth_token_secret'];
    setcookie("token_secret", " ", time()-3600);
    setcookie("token_secret", $token_secret, time()+60*10);
    setcookie("oauth_token", " ", time()-3600);
    setcookie("oauth_token", $oauth_token, time()+60*10);
    
    //GETTING THE URL FOR ASKING TWITTER TO AUTHORIZE THE APP WITH THE OAUTH TOKEN
    $url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));
    
    //REDIRECTING TO THE URL
    header('Location: ' . $url);
    
    ?>
    

    Second page : twitter-back.php

    The user gets redirected there from twitter once he authorizes the app. It then displays the user Access Token and the user Access Secret.

    oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier));
    
    $accessToken=$access_token['oauth_token'];
    $secretToken=$access_token['oauth_token_secret'];
    
    //DISPLAY THE TOKENS
    echo "Access Token : ".$accessToken."
    "; echo "Secret Token : ".$secretToken."
    "; ?>

    Please remember that you need to be using the using the TwitterOAuth library https://twitteroauth.com/

    Hope that helps ;)

    Arthur

    0 讨论(0)
提交回复
热议问题