-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathOAuth.php
More file actions
93 lines (85 loc) · 2.76 KB
/
OAuth.php
File metadata and controls
93 lines (85 loc) · 2.76 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
<?php
namespace DrewM\MailChimp;
/**
* Class OAuth
* This class allows one to use the oauth authentication of Mailchimp
* @package DrewM\MailChimp
*/
class OAuth
{
/**
* Get Mailchimp Authentication url
*
* @param $client_id
* @param $redirect_uri
* @return string
*/
public static function getAuthUrl($client_id, $redirect_uri){
$encoded_uri = urldecode($redirect_uri);
$authUrl = "https://login.mailchimp.com/oauth2/authorize";
$authUrl .= "?client_id=" . $client_id;
$authUrl .= "&redirect_uri=" . $encoded_uri;
$authUrl .= "&response_type=code";
return $authUrl;
}
/**
* Get a user access token from the code retrieved with getUrl
*
* @param $code
* @param $client_id
* @param $client_secret
* @param $redirect_uri
* @return string
*/
public static function getAccessToken($code, $client_id, $client_secret, $redirect_uri)
{
$encoded_uri = urldecode($redirect_uri);
$oauth_string = "grant_type=authorization_code";
$oauth_string .= "&client_id=" . $client_id;
$oauth_string .= "&client_secret=" . $client_secret;
$oauth_string .= "&redirect_uri=" . $encoded_uri;
$oauth_string .= "&code=" . $code;
return self::exchange($oauth_string);
}
/**
* Internal function that makes call to Mailchimp API to get an access token
*
* @param $oauth_string
* @return string
* @throws \Exception
*/
private static function exchange($oauth_string)
{
$ch = curl_init('https://login.mailchimp.com/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $oauth_string);
$return = curl_exec($ch);
if (!is_null(json_decode($return))) {
$return = json_decode($return);
}
curl_close($ch);
if (!$return->access_token) {
throw new \Exception(
'MailChimp did not return an access token',
$return
);
}
$headers = array('Authorization: OAuth ' . $return->access_token);
$ch = curl_init("https://login.mailchimp.com/oauth2/metadata/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$account = curl_exec($ch);
if (!is_null(json_decode($account))) {
$account = json_decode($account);
}
curl_close($ch);
if (!$account->dc) {
throw new \Exception(
'Unable to retrieve account meta-data',
$account
);
}
return $return->access_token . "-" . $account->dc;
}
}