Last active
November 1, 2022 02:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?