Skip to content

Commit 1ace6aa

Browse files
committed
Add Oauth
1 parent 917abe1 commit 1ace6aa

File tree

2 files changed

+90
-12
lines changed

2 files changed

+90
-12
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ The API uses "file_get_contents()" as HttpClient.
2727

2828
So it maybe will be crashed by SELinux.
2929

30-
Please turn off it, or using full version API.
30+
There are some solutions:
31+
+ Disable SELinux
32+
+ Add SELinux Policy
33+
+ To use [line-bot-sdk-php](https://github.com/line/line-bot-sdk-php)
3134

3235
Example
3336
--

api.php

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,79 @@ public function __construct($channelAccessToken, $channelSecret) {
6767
$this->channelSecret = $channelSecret;
6868
}
6969

70+
public function issueChannelAccessToken(){
71+
$header = array(
72+
"Content-Type: application/x-www-form-urlencoded"
73+
);
74+
75+
$content = array(
76+
"grant_type=client_credentials",
77+
"client_id=" . $channelId,
78+
"client_secret=" . $channelSecret
79+
);
80+
81+
$context = stream_context_create(array(
82+
"http" => array(
83+
"method" => "POST",
84+
"header" => implode("\r\n", $header),
85+
"content" => implode("\r\n", $content),
86+
),
87+
));
88+
89+
$response = file_get_contents($this->host.'/v2/oauth/accessToken', false, $context);
90+
if (strpos($http_response_header[0], '200') === false) {
91+
http_response_code(500);
92+
error_log("Request failed: " . $response);
93+
}else {
94+
$data = json_decode($response);
95+
$this->channelAccessToken = $data->access_token;
96+
return $data;
97+
}
98+
}
99+
100+
public function revokeChannelAccessToken(){
101+
$header = array(
102+
"Content-Type: application/x-www-form-urlencoded"
103+
);
104+
105+
$context = stream_context_create(array(
106+
"http" => array(
107+
"method" => "POST",
108+
"header" => implode("\r\n", $header),
109+
"content" => "access_token=" . $this->channelAccessToken,
110+
),
111+
));
112+
113+
$response = file_get_contents($this->host.'/v2/oauth/accessToken', false, $context);
114+
if (strpos($http_response_header[0], '200') === false) {
115+
http_response_code(500);
116+
error_log("Request failed: " . $response);
117+
}
118+
}
119+
120+
public function issueUserLinkToken($userId){
121+
$header = array(
122+
"Content-Type: application/json",
123+
'Authorization: Bearer ' . $this->channelAccessToken,
124+
);
125+
126+
$context = stream_context_create(array(
127+
"http" => array(
128+
"method" => "POST",
129+
"header" => implode("\r\n", $header),
130+
"content" => "[]",
131+
),
132+
));
133+
134+
$response = file_get_contents($this->host.'/v2/bot/user/'.urlencode($userId).'/linkToken', false, $context);
135+
if (strpos($http_response_header[0], '200') === false) {
136+
http_response_code(500);
137+
error_log("Request failed: " . $response);
138+
}else {
139+
return json_decode($response);
140+
}
141+
}
142+
70143
public function getProfile($userId) {
71144
$header = array(
72145
'Authorization: Bearer ' . $this->channelAccessToken,
@@ -84,7 +157,7 @@ public function getProfile($userId) {
84157
http_response_code(500);
85158
error_log("Request failed: " . $response);
86159
}else {
87-
return $response;
160+
return json_decode($response);
88161
}
89162
}
90163

@@ -105,13 +178,13 @@ public function getGroupMemberInfo($groupId, $userId) {
105178
http_response_code(500);
106179
error_log("Request failed: " . $response);
107180
}else {
108-
return $response;
181+
return json_decode($response);
109182
}
110183
}
111184

112185
public function getGroupMemberIds($groupId, $continuationToken = null) {
113186
if ($continuationToken != null) {
114-
$next = "?start=".$continuationToken;
187+
$next = "?start=" . $continuationToken;
115188
}else{
116189
$next = "";
117190
}
@@ -127,17 +200,18 @@ public function getGroupMemberIds($groupId, $continuationToken = null) {
127200
),
128201
));
129202

130-
$response = file_get_contents($this->host.'/v2/bot/group/'.urlencode($groupId).'/members/ids'.$next, false, $context);
203+
$response = file_get_contents($this->host.'/v2/bot/group/'.urlencode($groupId).'/members/ids'.urlencode($next), false, $context);
131204
if (strpos($http_response_header[0], '200') === false) {
132205
http_response_code(500);
133206
error_log("Request failed: " . $response);
134207
}else {
135-
return $response;
208+
return json_decode($response);
136209
}
137210
}
138211

139212
public function leaveGroup($groupId) {
140213
$header = array(
214+
"Content-Type: application/json",
141215
'Authorization: Bearer ' . $this->channelAccessToken,
142216
);
143217

@@ -173,13 +247,13 @@ public function getRoomMemberInfo($roomId, $userId) {
173247
http_response_code(500);
174248
error_log("Request failed: " . $response);
175249
}else {
176-
return $response;
250+
return json_decode($response);
177251
}
178252
}
179253

180254
public function getRoomMemberIds($roomId, $continuationToken = null) {
181255
if ($continuationToken != null) {
182-
$next = "?start=".$continuationToken;
256+
$next = "?start=" . $continuationToken;
183257
}else{
184258
$next = "";
185259
}
@@ -195,17 +269,18 @@ public function getRoomMemberIds($roomId, $continuationToken = null) {
195269
),
196270
));
197271

198-
$response = file_get_contents($this->host.'/v2/bot/room/'.urlencode($roomId).'/members/ids'.$next, false, $context);
272+
$response = file_get_contents($this->host.'/v2/bot/room/'.urlencode($roomId).'/members/ids' .urlencode($next), false, $context);
199273
if (strpos($http_response_header[0], '200') === false) {
200274
http_response_code(500);
201275
error_log("Request failed: " . $response);
202276
}else {
203-
return $response;
277+
return json_decode($response);
204278
}
205279
}
206280

207281
public function leaveRoom($roomId) {
208282
$header = array(
283+
"Content-Type: application/json",
209284
'Authorization: Bearer ' . $this->channelAccessToken,
210285
);
211286

@@ -362,7 +437,7 @@ public function getMessageObject($msgid) {
362437
),
363438
));
364439

365-
$response = file_get_contents($this->host."/v2/bot/message/".$msgid."/content", false, $context);
440+
$response = file_get_contents($this->host.'/v2/bot/message/' .urlencode($msgid).'/content', false, $context);
366441
if (strpos($http_response_header[0], '200') === false) {
367442
http_response_code(500);
368443
error_log("Request failed: " . $response);
@@ -374,7 +449,7 @@ public function getMessageObject($msgid) {
374449
public function downloadMessageObject($msgid, $path = "./") {
375450
$response = $this->getMessageObject($msgid);
376451
if ($response != null) {
377-
$file = fopen($path.$msgid, "wb");
452+
$file = fopen($path . $msgid, "wb");
378453
fwrite($file, $response);
379454
fclose($file);
380455
}else{

0 commit comments

Comments
 (0)