-
-
Save bearburger/b4d1a058c4f85b75fa83 to your computer and use it in GitHub Desktop.
Periscope Login via Twitter process
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 | |
/* | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
READ http://stackoverflow.com/a/34474400/580709 FIRST | |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
1. This is example script so I don't care about code quality and your opinion about it. | |
2. This script should be launched from command line. | |
3. You need Periscope application consumer keys to use this script. | |
4. You can't create your own Twitter application and use its keys to access Periscope. | |
5. Don't ask me for consumer keys! | |
6. If you see errors then your keys are wrong. | |
*/ | |
$twitter_consumer_key = ''; | |
$twitter_consumer_secret = ''; | |
$user_agent = ''; | |
function proper_parse_str($str) { | |
$arr = array(); | |
$pairs = explode('&', $str); | |
foreach ($pairs as $i) { | |
list($name,$value) = explode('=', $i, 2); | |
if( isset($arr[$name]) ) { | |
if( is_array($arr[$name]) ) { | |
$arr[$name][] = $value; | |
} else { | |
$arr[$name] = array($arr[$name], $value); | |
} | |
} else { | |
$arr[$name] = $value; | |
} | |
} | |
return $arr; | |
} | |
function oauth_request($resource, $parameters, $consumer_key, $consumer_secret, $oauth_token_secret = '') { | |
$parameters['oauth_version'] = '1.0'; | |
$parameters['oauth_consumer_key'] = $consumer_key; | |
$parameters['oauth_signature_method'] = 'HMAC-SHA1'; | |
$parameters['oauth_nonce'] = base64_encode(time()); | |
$parameters['oauth_timestamp'] = time(); | |
ksort($parameters); | |
$oauth_signature = http_build_query($parameters); | |
$oauth_signature = 'POST&' . urlencode($resource) . '&' . urlencode($oauth_signature); | |
$key = $consumer_secret . '&'; | |
if (!empty($oauth_token_secret)) { | |
$key .= '&' . urlencode($oauth_token_secret); | |
} | |
$oauth_signature = base64_encode(hash_hmac('sha1', $oauth_signature, $key, TRUE)); | |
$parameters['oauth_signature'] = $oauth_signature; | |
ksort($parameters); | |
$header = []; | |
foreach($parameters as $k => $v) { | |
$header[] = $k . '="' . urlencode($v) . '"'; | |
} | |
$header = 'OAuth ' . implode(', ', $header); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $resource); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Authorization: ' . $header, | |
'Accept: ', | |
'User-Agent: ', | |
'Expect: ' | |
)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
return $data; | |
} | |
// STEP 1 | |
$oauth_data = oauth_request("https://api.twitter.com/oauth/request_token", [ | |
'oauth_callback' => 'twittersdk://callback?version=1.5.0.78&app=' . $twitter_consumer_key | |
], $twitter_consumer_key, $twitter_consumer_secret); | |
$oauth_data = proper_parse_str($oauth_data); | |
// STEP 2 | |
echo "Please, open in browser URL below\n"; | |
echo "https://api.twitter.com/oauth/authorize?oauth_token=" . $oauth_data['oauth_token'] . "\n"; | |
// STEP 3 | |
if (PHP_OS == 'WINNT') { | |
echo "Paste redirect URL: "; | |
$redirect_data = stream_get_line(STDIN, 1024, PHP_EOL); | |
} else { | |
$redirect_data = readline("Paste redirect URL: "); | |
} | |
$redirect_data = proper_parse_str($redirect_data); | |
// STEP 4 | |
$user_data = oauth_request("https://api.twitter.com/oauth/access_token?oauth_verifier=" . $redirect_data['oauth_verifier'], [ | |
'oauth_token' => $oauth_data['oauth_token'] | |
], $twitter_consumer_key, $twitter_consumer_secret, $oauth_data['oauth_token_secret']); | |
$user_data = proper_parse_str($user_data); | |
// STEP 5 | |
$auth_data = array( | |
'bundle_id' => 'com.bountylabs.periscope', | |
'phone_number' => '', | |
'session_key' => $user_data['oauth_token'], | |
'session_secret' => $user_data['oauth_token_secret'], | |
'user_id' => $user_data['user_id'], | |
'user_name' => $user_data['screen_name'], | |
'vendor_id' => '81EA8A9B-2950-40CD-9365-40535404DDE4', | |
); | |
$auth_data = json_encode($auth_data); | |
$ch = curl_init("https://api.periscope.tv/api/v2/loginTwitter"); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $auth_data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json; charset=UTF-8', | |
'User-Agent: ' . $user_agent, | |
'Expect: ' | |
)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$periscope_user = curl_exec($ch); | |
curl_close($ch); | |
$periscope_user = json_decode($periscope_user); | |
echo 'Periscope authorization cookie: ' . $periscope_user->cookie; |
I am also facing the issue Response: Unauthorized, Status: 401, Please Give me a Solution for that
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am facing the issue Response: Unauthorized, Status: 401, please anyone guide me ?