-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterTokenFactory.php
52 lines (40 loc) · 1.27 KB
/
TwitterTokenFactory.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
<?php
namespace Antanova\Wordpress;
class TwitterTokenFactory
{
protected $api_key;
protected $secret;
protected $headers = array();
protected $url = 'https://api.twitter.com/oauth2/token';
public function __construct($key, $secret)
{
$this->api_key = $key;
$this->secret = $secret;
$this->setAuthorizationHeader();
}
public function setAuthorizationHeader()
{
$token = base64_encode(rawurlencode($this->api_key).':'.rawurlencode($this->secret));
$this->headers['Authorization'] = "Basic $token";
}
/**
* Create our bearer token.
*
* @return array The response from the twitter server
*/
public function createToken()
{
$request_body = array(
'grant_type' => 'client_credentials',
);
$request = \wp_safe_remote_post($this->url, array(
'body' => $request_body,
'headers' => $this->headers,
));
if (is_wp_error($request)) {
//return new \WP_Error('token_error', 'There was a problem requesting a Twitter token: ' . $request->get_error_message());
die('There was a problem requesting a Twitter token: '.$request->get_error_message());
}
return $request;
}
}