-
Notifications
You must be signed in to change notification settings - Fork 15
/
refacctok.php
97 lines (87 loc) · 3.57 KB
/
refacctok.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
require 'globals.php';
require 'oauth_helper.php';
// Fill in the next 3 variables.
$old_access_token='REPLACE_ME';
$old_token_secret='REPLACE_ME';
$oauth_session_handle='REPLACE_ME';
// Refresh the access token using HTTP GET and HMAC-SHA1 signature
$retarr = refresh_access_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
$old_access_token, $old_token_secret,
$oauth_session_handle, false, true, true);
if (! empty($retarr)) {
list($info, $headers, $body, $body_parsed) = $retarr;
if ($info['http_code'] == 200 && !empty($body)) {
print "Use oauth_token as the token for all of your API calls:\n" .
rfc3986_decode($body_parsed['oauth_token']) . "\n";
}
}
exit(0);
/**
* Refresh an access token using an expired request token
* @param string $consumer_key obtained when you registered your app
* @param string $consumer_secret obtained when you registered your app
* @param string $old_access_token obtained previously
* @param string $old_token_secret obtained previously
* @param string $oauth_session_handle obtained previously
* @param bool $usePost use HTTP POST instead of GET (default false)
* @param bool $useHmacSha1Sig use HMAC-SHA1 signature (default false)
* @return response string with token or empty array on error
*/
function refresh_access_token($consumer_key, $consumer_secret, $old_access_token, $old_token_secret, $oauth_session_handle, $usePost=false, $useHmacSha1Sig=true, $passOAuthInHeader=true)
{
$retarr = array(); // return value
$response = array();
$url = 'https://api.login.yahoo.com/oauth/v2/get_token';
$params['oauth_version'] = '1.0';
$params['oauth_nonce'] = mt_rand();
$params['oauth_timestamp'] = time();
$params['oauth_consumer_key'] = $consumer_key;
$params['oauth_token'] = $old_access_token;
$params['oauth_session_handle'] = $oauth_session_handle;
// compute signature and add it to the params list
if ($useHmacSha1Sig) {
$params['oauth_signature_method'] = 'HMAC-SHA1';
$params['oauth_signature'] =
oauth_compute_hmac_sig($usePost? 'POST' : 'GET', $url, $params,
$consumer_secret, $old_token_secret);
} else {
$params['oauth_signature_method'] = 'PLAINTEXT';
$params['oauth_signature'] =
oauth_compute_plaintext_sig($consumer_secret, $old_token_secret);
}
// Pass OAuth credentials in a separate header or in the query string
if ($passOAuthInHeader) {
$query_parameter_string = oauth_http_build_query($params, true);
$header = build_oauth_header($params, "yahooapis.com");
$headers[] = $header;
} else {
$query_parameter_string = oauth_http_build_query($params);
}
// POST or GET the request
if ($usePost) {
$request_url = $url;
logit("refacctok:INFO:request_url:$request_url");
logit("refacctok:INFO:post_body:$query_parameter_string");
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$response = do_post($request_url, $query_parameter_string, 443, $headers);
} else {
$request_url = $url . ($query_parameter_string ?
('?' . $query_parameter_string) : '' );
logit("refacctok:INFO:request_url:$request_url");
$response = do_get($request_url, 443, $headers);
}
// extract successful response
if (! empty($response)) {
list($info, $header, $body) = $response;
$body_parsed = oauth_parse_str($body);
if (! empty($body_parsed)) {
logit("getacctok:INFO:response_body_parsed:");
print_r($body_parsed);
}
$retarr = $response;
$retarr[] = $body_parsed;
}
return $retarr;
}
?>