-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgetOauthAccessKeyAndSecret.php
More file actions
152 lines (114 loc) · 4.53 KB
/
getOauthAccessKeyAndSecret.php
File metadata and controls
152 lines (114 loc) · 4.53 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
$email = $_POST['email'];
$password = $_POST[pass];
function getOauthAccessKeyAndSecret($oauthConsumerKey,$oauthConsumerSecret,$username,$password,$baseurl){
//initiate
$realm = $baseurl;
$endpointUrl = $realm."oauth/initiate";
$oauthCallback = $baseurl;
$oauthNonce = uniqid(mt_rand(1, 1000));
$oauthSignatureMethod = "HMAC-SHA1";
$oauthTimestamp = time();
$oauthVersion = "1.0";
$oauthMethod = "POST";
$params = array(
"oauth_callback" => $oauthCallback,
"oauth_consumer_key" => $oauthConsumerKey,
"oauth_nonce" => $oauthNonce,
"oauth_signature_method" => $oauthSignatureMethod,
"oauth_timestamp" => $oauthTimestamp,
"oauth_version" => $oauthVersion,
);
$data = http_build_query($params);
$encodedData = $oauthMethod."&".urlencode($endpointUrl)."&".urlencode($data);
$key = $oauthConsumerSecret."&";
$signature = hash_hmac("sha1",$encodedData, $key, 1);
$oauthSignature = base64_encode($signature);
$header = "Authorization: OAuth realm=\"$realm\",";
foreach ($params as $key=>$value){
$header .= $key.'="'.$value."\", ";
}
$header .= "oauth_signature=\"".$oauthSignature.'"';
var_dump($header);
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header));
curl_setopt($curl, CURLOPT_URL, $endpointUrl);
$response = curl_exec($curl);
curl_close($curl);
$response = explode('&',$response);
$key = explode('=',$response[0]);
$secret = explode('=',$response[1]);
$oauthkey = $key[1];
$oauthsecret = $secret[1];
echo $oauthkey.' '.$oauthsecret."\n";
//authorize
$url = $baseurl.'oauth/authorize?oauth_token='.$oauthkey.'&username='.$username.'&password='.$password;
$curl = curl_init();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch); // $a will contain all headers
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
$url = explode('&',$url);
$url = explode('=',$url[1]);
$verifier = $url[1];
//oauth access
$endpointUrl = $realm."oauth/token";
$params2 = array(
'oauth_consumer_key' => $oauthConsumerKey,
'oauth_nonce' => uniqid(mt_rand(1, 1000)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0',
//'oauth_token' => 'token',
//'oauth_verifier' => 'verifier',
'oauth_token' => $oauthkey,
'oauth_verifier' => $verifier,
);
$method = 'POST';
// this is the url to get Request Token according to Magento doc
$url = $endpointUrl;
// start making the signature
ksort($params2); // @see Zend_Oauth_Signature_SignatureAbstract::_toByteValueOrderedQueryString() for more accurate sorting, including array params
$sortedParamsByKeyEncodedForm = array();
foreach ($params2 as $key => $value) {
$sortedParamsByKeyEncodedForm[] = rawurlencode($key) . '=' . rawurlencode($value);
}
$strParams = implode('&', $sortedParamsByKeyEncodedForm);
$signatureData = strtoupper($method) // HTTP method (POST/GET/PUT/...)
. '&'
. rawurlencode($url) // base resource url - without port & query params & anchors, @see how Zend extracts it in Zend_Oauth_Signature_SignatureAbstract::normaliseBaseSignatureUrl()
. '&'
. rawurlencode($strParams);
$key = rawurlencode($oauthConsumerSecret) . '&' . rawurlencode($oauthsecret);
$oauthSignature = base64_encode(hash_hmac('SHA1', $signatureData, $key, 1));
$header = "Authorization: OAuth realm=\"$realm\",";
foreach ($params2 as $key=>$value){
$header .= $key.'="'.$value."\", ";
}
$header .= "oauth_signature=\"".$oauthSignature.'"';
//var_dump($header);
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header));
curl_setopt($curl, CURLOPT_URL, $endpointUrl);
$response = curl_exec($curl);
curl_close($curl);
$response = explode('&',$response);
$access_key = explode('=',$response[0]);
$access_key = $access_key[1];
$access_secret = explode('=',$response[1]);
$access_secret = $access_secret[1];
echo $access_key.','.$access_secret;
}
getOauthAccessKeyAndSecret($key,$secrect ,$email, $password,'http://domain.com');