Skip to content

Instantly share code, notes, and snippets.

@levymetal
Last active November 1, 2022 02:40
Show Gist options
  • Save levymetal/5891399 to your computer and use it in GitHub Desktop.
Save levymetal/5891399 to your computer and use it in GitHub Desktop.
Obtain a bearer token from Twitter, which can use used to send signed requests without the need for an oauth library.
<?php
$ch = curl_init();
//set the endpoint url
curl_setopt($ch,CURLOPT_URL, 'https://api.twitter.com/oauth2/token');
// has to be a post
curl_setopt($ch,CURLOPT_POST, true);
$data = array();
$data['grant_type'] = "client_credentials";
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
// here's where you supply the Consumer Key / Secret from your app:
$consumerKey = 'xxxxxxxxx';
$consumerSecret = 'yyyyyyyyyyyyyyyyyyy';
curl_setopt($ch,CURLOPT_USERPWD, $consumerKey . ':' . $consumerSecret);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
// show the result, including the bearer token (or you could parse it and stick it in a DB)
print_r($result);
@Aruw
Copy link

Aruw commented Aug 29, 2017

I only need to paste the $consumerKey and $consumerSecret? Do I need to modify anything else?
How do I run this? Shouldn't it be in a HTML file? Can you help me please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment