-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-oauth1.php
More file actions
85 lines (72 loc) · 2.33 KB
/
example-oauth1.php
File metadata and controls
85 lines (72 loc) · 2.33 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
<?php
/**
* A full self-contained OAuth1 example
*
* @created 19.05.2024
* @author smiley <smiley@chillerlan.net>
* @copyright 2024 smiley
* @license MIT
*/
declare(strict_types=1);
use chillerlan\OAuth\Core\OAuthInterface;
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\Providers\Discogs;
use chillerlan\OAuth\Storage\SessionStorage;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
require_once __DIR__.'/../vendor/autoload.php';
#error_reporting(E_ALL);
#ini_set('display_errors', 1);
ini_set('date.timezone', 'UTC');
// invoke the oauth options instance
$options = new OAuthOptions([
'key' => '[client id]',
'secret' => '[client secret]',
'callbackURL' => '[callback URL]',
'sessionStart' => true,
]);
// the PSR-18 HTTP client
$http = new Client([
'verify' => '/path/to/cacert.pem',
'headers' => [
'User-Agent' => OAuthInterface::USER_AGENT,
],
]);
// the PSR-17 factory/factories
$httpFactory = new HttpFactory;
// the storage instance
$storage = new SessionStorage($options);
// the provider
$provider = new Discogs($options, $http, $httpFactory, $httpFactory, $httpFactory, $storage);
// execute the oauth flow
$name = $provider->getName();
// step 2: redirect to the provider's login screen
if(isset($_GET['login']) && $_GET['login'] === $name){
header('Location: '.$provider->getAuthorizationURL());
}
// step 3: receive the access token
elseif(isset($_GET['oauth_token'], $_GET['oauth_verifier'])){
$token = $provider->getAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);
// save the token in a permanent storage
// [...]
// access granted, redirect
header('Location: ?granted='.$name);
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
// use the permanent storage from now on
// [...]
// dump the AuthenticatedUser instance
printf('<pre>%s</pre>', print_r($provider->me(), true));
// convert the token to JSON and display it
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();
printf('<textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $tokenJSON);
}
// bonus: handle errors
elseif(isset($_GET['error'])){
throw new RuntimeException($_GET['error']);
}
// step 1 (optional): display a login link
else{
echo '<a href="?login='.$name.'">Connect with '.$name.'!</a>';
}