thephpleague / oauth1-client

OAuth 1 Client
MIT License
968 stars 73 forks source link

Add email to Twitter #25

Closed sitesense closed 9 years ago

sitesense commented 9 years ago

Since April of this year Twitter is now able to supply a user's email address, with one caveat - you need to have your app whitelisted. See the post by andypiper of Twitter here:

https://twittercommunity.com/t/how-to-get-email-from-twitter-user-using-oauthtokens/558/185

I got this to work by making a few minor changes to src/Client/Server/Twitter.php - look for CHANGED...

I haven't made a PR because I think you guys will probably have better ideas on how to implement it.

public function urlUserDetails()
    {
        // return 'https://api.twitter.com/1.1/account/verify_credentials.json';
        // CHANGED added querystring
        return 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true';
    }

    /**
     * {@inheritDoc}
     */
    public function userDetails($data, TokenCredentials $tokenCredentials)
    {
        $user = new User();

        $user->uid = $data['id'];
        $user->nickname = $data['screen_name'];
        $user->name = $data['name'];

        // CHANGED added email
        $user->email = null;
        if (isset($data['email'])) {
            $user->email = $data['email'];
        }

        $user->location = $data['location'];
        $user->description = $data['description'];
        $user->imageUrl = $data['profile_image_url'];

        // CHANGED added email
        //$used = array('id', 'screen_name', 'name', 'location', 'description', 'profile_image_url');
        $used = array('id', 'screen_name', 'name', 'email', 'location', 'description', 'profile_image_url');

        foreach ($data as $key => $value) {
            if (strpos($key, 'url') !== false) {
                if (!in_array($key, $used)) {
                    $used[] = $key;
                }

                $user->urls[$key] = $value;
            }
        }

        // Save all extra data
        $user->extra = array_diff_key($data, array_flip($used));

        return $user;
    }
bencorlett commented 9 years ago

I like that implementation on the face of it. Can you submit a PR? I'll take a look on the computer but credit should be where credit is due!

Sent from my iPhone

Please excuse my brevity

On 17 May 2015, at 5:29 am, Mark notifications@github.com wrote:

Since April of this year Twitter is now able to supply a user's email address, with one caveat - you need to have your app whitelisted. See the post by andypiper of Twitter here:

https://twittercommunity.com/t/how-to-get-email-from-twitter-user-using-oauthtokens/558/185

I got this to work by making a few minor changes to src/Client/Server/Twitter.php - look for CHANGED...

I haven't made a PR because I think you guys will probably have better ideas on how to implement it.

` public function urlUserDetails() { // return 'https://api.twitter.com/1.1/account/verify_credentials.json'; // CHANGED added querystring return 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true'; }

/**

  • {@inheritDoc} */ public function userDetails($data, TokenCredentials $tokenCredentials) { $user = new User();

    $user->uid = $data['id']; $user->nickname = $data['screen_name']; $user->name = $data['name'];

    // CHANGED added email $user->email = null; if (isset($data['email'])) { $user->email = $data['email']; }

    $user->location = $data['location']; $user->description = $data['description']; $user->imageUrl = $data['profile_image_url'];

    // CHANGED added email //$used = array('id', 'screen_name', 'name', 'location', 'description', 'profile_image_url'); $used = array('id', 'screen_name', 'name', 'email', 'location', 'description', 'profile_image_url');

    foreach ($data as $key => $value) { if (strpos($key, 'url') !== false) { if (!in_array($key, $used)) { $used[] = $key; }

      $user->urls[$key] = $value;
    }

    }

    // Save all extra data $user->extra = array_diff_key($data, array_flip($used));

    return $user; } `

— Reply to this email directly or view it on GitHub.

sitesense commented 9 years ago

Ok, thanks Ben, I'll do that.