Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Important Updated needed for Doc, about get user Data > email #300

Closed
renaudham opened this issue Mar 17, 2022 · 3 comments
Closed

Important Updated needed for Doc, about get user Data > email #300

renaudham opened this issue Mar 17, 2022 · 3 comments

Comments

@renaudham
Copy link

You really need to explain how exactly to get the user data for Sign In with TW

and precise that at the end it requires to be exactly in this order, to work, with setGetfield first in the order logic
I lost hours before being able to find
I can send for you the whole steps to reach this..

Anyway the most difficult part was the end last step :

               $url = 'https://api.twitter.com/1.1/account/verify_credentials.json'; 

$getfields="?include_email=true";//&oauth_token=".$response_array["oauth_token"];
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$result = $twitter->setGetfield($getfields)
->buildOauth($url, $requestMethod)
->performRequest();

@JLuc
Copy link

JLuc commented Mar 17, 2022

Yes !
Please post the whole steps about how exactly to get the user data for Sign In with TW

@renaudham
Copy link
Author

renaudham commented Mar 17, 2022

STEP 1 -- CALL THE API TO GHET REDIRECTION URL

public $settingsTwitter = array(
       'oauth_access_token' => "xxxxxx",
       'oauth_access_token_secret' => "xxxxxx",
       'consumer_key' => "xxxxxx",
       'consumer_secret' => "xxxxxx"
   );
 public function login_twitter()
   {
       session_start();
       $redirect_url = "{{YOUR OWN REDIRECTION URL"; //The page where Twitter will redirect after you login and accepted login with Twitter
       $settings = $this->settingsTwitter;
       $postfields = array(
           'oauth_callback' => rawurlencode($redirect_url)
       );
       $url = "https://api.twitter.com/oauth/request_token";
       $requestMethod = 'POST';
       $twitter = new TwitterAPIExchange($settings);
       $result = $twitter->buildOauth($url, $requestMethod)
           ->setPostfields($postfields)
           ->performRequest();
       $result = explode('&', $result);

       $request_token = array(
           "oauth_token" => explode('=', $result[0])[1],
           "oauth_token_secret" => explode('=', $result[1])[1],
           "oauth_callback_confirmed" => explode('=', $result[2])[1],
       );
       if ($request_token["oauth_callback_confirmed"] === "true") {
          $redirection_url = 'https://api.twitter.com/oauth/authenticate?oauth_token=' . $request_token["oauth_token"];
          //Here, redirect directly to this TwitterURL, or send it back to be processed on Front (for API system)
          //Once passed Twitter login, it will redirect to $redirect_url 
       } else {
           //Show error message
       }
}

//STEP 2
in a page where you are redicted to by Twitter
That should be the one in $redirect_url


if (isset($_GET['oauth_verifier']) && isset($_GET['oauth_token'])) {

            $oauth_token = $_GET['oauth_token'];
            $oauth_verifier = $_GET['oauth_verifier'];

           
            $settings = $this->settingsTwitter;

//QUERY ACCESS TOKEN
            $postfields = array(
                'oauth_verifier' => $oauth_verifier,
                'oauth_token' => $oauth_token,
            );
            $url = 'https://api.twitter.com/oauth/access_token';
            $requestMethod = 'POST';
            $twitter = new TwitterAPIExchange($settings);
            $result = $twitter->buildOauth($url, $requestMethod)
                ->setPostfields($postfields)
                ->performRequest();
          
            $result = explode('&', $result);
            $response_array = array(
                "oauth_token" => explode('=', $result[0])[1],
                "oauth_token_secret" => explode('=', $result[1])[1],
                "user_id" => explode('=', $result[2])[1],
                "screen_name" => explode('=', $result[3])[1],
            );
         
//QUERY USER DATA

            $url = 'https://api.twitter.com/1.1/account/verify_credentials.json';
           
            $getfields = "?include_email=true";//&oauth_token=".$response_array["oauth_token"];
            $requestMethod = 'GET';
            $twitter = new TwitterAPIExchange($settings);
            $result = $twitter->setGetfield($getfields)
                ->buildOauth($url, $requestMethod)
                ->performRequest();
            $userinfo = json_decode($result);
            if (isset($userinfo->email)) {
                $user = (object)array(
                    "email" => $userinfo->email,
                    "name" => $userinfo->name,
                    "id" => $userinfo->id,
                );
                ///HERE PROCESS LOGIN USER IN YOUR CMS
                exit;
            }else {
                //HERE WARN ERROR
            }
        } else {
                //HERE WARN ERROR
        }

@renaudham
Copy link
Author

I have added a Medium blog post where it's more detaillled and more clear
https://blueorigamidigital.medium.com/php-twitter-sign-in-register-with-twitter-api-and-the-sdk-twitter-api-php-ee68de56aae1

@J7mbo J7mbo closed this as completed Jan 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants