8
8
9
9
class Provider extends AbstractProvider
10
10
{
11
+ /**
12
+ * Unique Provider Identifier.
13
+ */
11
14
public const IDENTIFIER = 'INSTAGRAM ' ;
12
15
13
16
/**
14
17
* {@inheritdoc}
15
18
*/
16
19
protected $ scopeSeparator = ' ' ;
17
20
21
+ /**
22
+ * The user fields being requested.
23
+ *
24
+ * @var array
25
+ */
26
+ protected $ fields = ['account_type ' , 'id ' , 'username ' , 'media_count ' ];
27
+
18
28
/**
19
29
* {@inheritdoc}
20
30
*/
21
- protected $ scopes = ['basic ' ];
31
+ protected $ scopes = ['user_profile ' ];
22
32
23
33
/**
24
34
* {@inheritdoc}
@@ -44,24 +54,19 @@ protected function getTokenUrl()
44
54
*/
45
55
protected function getUserByToken ($ token )
46
56
{
47
- $ endpoint = '/users/self ' ;
48
- $ query = [
49
- 'access_token ' => $ token ,
50
- ];
51
- $ signature = $ this ->generateSignature ($ endpoint , $ query );
52
-
53
- $ query ['sig ' ] = $ signature ;
54
- $ response = $ this ->getHttpClient ()->get (
55
- 'https://api.instagram.com/v1/users/self ' ,
56
- [
57
- RequestOptions::QUERY => $ query ,
58
- RequestOptions::HEADERS => [
59
- 'Accept ' => 'application/json ' ,
60
- ],
61
- ]
62
- );
57
+ $ meUrl = 'https://graph.instagram.com/me?access_token= ' .$ token .'&fields= ' .implode (', ' , $ this ->fields );
58
+
59
+ if (!empty ($ this ->clientSecret )) {
60
+ $ appSecretProof = hash_hmac ('sha256 ' , $ token , $ this ->clientSecret );
61
+ $ meUrl .= '&appsecret_proof= ' .$ appSecretProof ;
62
+ }
63
+ $ response = $ this ->getHttpClient ()->get ($ meUrl , [
64
+ RequestOptions::HEADERS => [
65
+ 'Accept ' => 'application/json ' ,
66
+ ],
67
+ ]);
63
68
64
- return json_decode (( string ) $ response ->getBody (), true )[ ' data ' ] ;
69
+ return json_decode ($ response ->getBody (), true );
65
70
}
66
71
67
72
/**
@@ -70,9 +75,10 @@ protected function getUserByToken($token)
70
75
protected function mapUserToObject (array $ user )
71
76
{
72
77
return (new User ())->setRaw ($ user )->map ([
73
- 'id ' => $ user ['id ' ], 'nickname ' => $ user ['username ' ],
74
- 'name ' => $ user ['full_name ' ], 'email ' => null ,
75
- 'avatar ' => $ user ['profile_picture ' ],
78
+ 'id ' => $ user ['id ' ],
79
+ 'name ' => $ user ['username ' ],
80
+ 'account_type ' => $ user ['account_type ' ],
81
+ 'media_count ' => $ user ['media_count ' ] ?? null ,
76
82
]);
77
83
}
78
84
@@ -96,23 +102,4 @@ protected function getTokenFields($code)
96
102
'grant_type ' => 'authorization_code ' ,
97
103
]);
98
104
}
99
-
100
- /**
101
- * Allows compatibility for signed API requests.
102
- *
103
- * @param string @endpoint
104
- * @param array $params
105
- *
106
- * @return string
107
- */
108
- protected function generateSignature ($ endpoint , array $ params )
109
- {
110
- $ sig = $ endpoint ;
111
- ksort ($ params );
112
- foreach ($ params as $ key => $ val ) {
113
- $ sig .= "| $ key= $ val " ;
114
- }
115
-
116
- return hash_hmac ('sha256 ' , $ sig , $ this ->clientSecret , false );
117
- }
118
105
}
0 commit comments