-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.php
executable file
·72 lines (58 loc) · 1.98 KB
/
auth.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
<html>
<head>
</head>
<body>
<?php
// Tokens
$keys = array(
"clientid"=>"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
"clientsecret"=>"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
);
// URLs for authorizing, etc.
$uris = array(
"authorize"=>"https://www.mapmyfitness.com/v7.1/oauth2/authorize/",
"redirect"=>"https://biking.michaelborn.me/apis/MapMyRide.php",
"accesstoken"=>"https://api.mapmyfitness.com/v7.1/oauth2/access_token/"
);
// Get the access token and refresh token from the API
// based on the url code provided
if (isset($_GET["code"])) {
$authFields = array(
"grant_type"=>"authorization_code",
"client_id"=>$keys["clientid"],
"client_secret"=>$keys["clientsecret"],
"code"=>$_GET["code"]
);
//url-ify the data for the POST
foreach($authFields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $uris["accesstoken"]);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
// "Data must be sent with a Content-Type of application/x-www-form-urlencoded"
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
//execute post
$result = curl_exec($ch);
// hopefully, we get a string of tokens.
print "<h1>Access tokens</h1>";
print "<p>Please save these (in tokens.json) for later use.</p>";
var_dump($result);
//close connection
curl_close($ch);
} else {
?>
<form method="GET" action="<?= $uris["authorize"] ?>">
Client Key: <input type="text" name="client_id" value="<?= $keys["clientid"] ?>" /><br />
Response Type: <input type="text" name="response_type" value="code" /><br />
<br />
Redirect URI: <input type="text" name="redirect_uri" value="<?= $uris["redirect"] ?>" /><br />
<input type="submit" />
</form>
<?php
}
?>
</body>
</html>