teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

記述変更ご

2018/06/03 13:04

投稿

FigHy
FigHy

スコア19

title CHANGED
File without changes
body CHANGED
@@ -26,10 +26,20 @@
26
26
 
27
27
  ご教授お願い致します。
28
28
 
29
- 追記
29
+ 追記→変更後
30
30
 
31
31
  ```
32
+ <?php
33
+
34
+ namespace App\Socialite;
35
+
36
+ use Laravel\Socialite\Two\AbstractProvider;
37
+ use Laravel\Socialite\Two\ProviderInterface;
38
+ use Laravel\Socialite\Two\User;
39
+
40
+ class LineProvider extends AbstractProvider implements ProviderInterface
41
+ {
32
- protected function getAuthUrl($state)
42
+ protected function getAuthUrl($state)
33
43
  {
34
44
  return $this->buildAuthUrlFromBase('https://access.line.me/oauth2/v2.1/authorize', $state);
35
45
  }
@@ -44,30 +54,54 @@
44
54
  {
45
55
  $response = $this->getHttpClient()->get('https://api.line.me/v2/profile', [
46
56
  'headers' => [
47
- 'X-Line-ChannelToken' => $token,
57
+ 'Authorization' => 'Bearer '.$token,
48
58
  ],
49
59
  ]);
50
-
51
- return json_decode($response->getBody(), true);
60
+ return json_decode($response->getBody()->getContents(), true);
52
61
  }
53
62
 
54
63
  protected function mapUserToObject(array $user)
55
64
  {
56
65
  return (new User())->setRaw($user)->map([
66
+ 'id' => $user['userId'] ?? $user['sub'] ?? null,
67
+ 'nickname' => null,
68
+ 'name' => $user['displayName'] ?? $user['name'] ?? null,
69
+ 'avatar' => $user['pictureUrl'] ?? $user['picture'] ?? null,
57
- 'id' => $user['mid'],
70
+ 'email' => $user['email'] ?? null,
58
- 'name' => $user['displayName'],
59
- 'avatar' => $user['pictureUrl'],
60
71
  ]);
61
72
  }
62
73
 
63
74
  protected function getTokenFields($code)
64
75
  {
65
- return [
66
- 'client_id' => $this->clientId,
67
- 'client_secret' => $this->clientSecret,
68
- 'code' => $code,
69
- 'redirect_uri' => $this->redirectUrl,
76
+ return array_merge(parent::getTokenFields($code), [
70
- 'grant_type' => 'authorization_code',
77
+ 'grant_type' => 'authorization_code',
71
- ];
78
+ ]);
72
79
  }
80
+
81
+ /**
82
+ * @return \SocialiteProviders\Manager\OAuth2\User
83
+ */
84
+ public function user()
85
+ {
86
+ if ($this->hasInvalidState()) {
87
+ throw new InvalidStateException();
88
+ }
89
+ $response = $this->getAccessTokenResponse($this->getCode());
90
+ if ($jwt = $response['id_token'] ?? null) {
91
+ list($headb64, $bodyb64, $cryptob64) = explode('.', $jwt);
92
+ $user = $this->mapUserToObject(json_decode(base64_decode($bodyb64), true));
93
+ } else {
94
+ $user = $this->mapUserToObject($this->getUserByToken(
95
+ $token = $this->parseAccessToken($response)
96
+ ));
97
+ }
98
+ $this->credentialsResponseBody = $response;
99
+ if ($user instanceof User) {
100
+ $user->setAccessTokenResponseBody($this->credentialsResponseBody);
101
+ }
102
+ return $user->setToken($this->parseAccessToken($response))
103
+ ->setRefreshToken($this->parseRefreshToken($response))
104
+ ->setExpiresIn($this->parseExpiresIn($response));
105
+ }
106
+ }
73
107
  ```

4

2018/06/03 13:04

投稿

FigHy
FigHy

スコア19

title CHANGED
@@ -1,1 +1,1 @@
1
- Laravel Line ログイン認証機能でのエラー
1
+ Laravel Line Socialite ログイン認証機能でのエラー
body CHANGED
File without changes

3

エラー文追加

2018/06/03 04:49

投稿

FigHy
FigHy

スコア19

title CHANGED
File without changes
body CHANGED
@@ -3,9 +3,14 @@
3
3
  laravelでLineのログイン認証を実装しているのですが、
4
4
  下記のエラーが出て詰まっています。
5
5
 
6
+ エラー文
6
7
  ```
7
8
  Client error: `POST https://api.line.me/oauth2/v2.1/token` resulted in a `400 Bad Request` response: {"error":"invalid_request","error_description":"code is required."}
8
9
  ```
10
+ エラー箇所
11
+ ```
12
+ return new $className($message, $request, $response, $previous, $ctx);
13
+ ```
9
14
 
10
15
  コード的にはここで詰まっているみたいなので、トークンの問題なのかと思うのですが...
11
16
  ```

2

リクエスト追記

2018/06/03 00:03

投稿

FigHy
FigHy

スコア19

title CHANGED
File without changes
body CHANGED
@@ -19,4 +19,50 @@
19
19
  自分のLineアカウントで、なんどもログインしてはコードをいじるの繰り返しをしているので、そのせいでしょうか...
20
20
  トークンを取り消せばいいのかと思うのですが、やり方がよくわからず。
21
21
 
22
- ご教授お願い致します。
22
+ ご教授お願い致します。
23
+
24
+ 追記
25
+
26
+ ```
27
+ protected function getAuthUrl($state)
28
+ {
29
+ return $this->buildAuthUrlFromBase('https://access.line.me/oauth2/v2.1/authorize', $state);
30
+ }
31
+
32
+ protected function getTokenUrl()
33
+ {
34
+ return 'https://api.line.me/oauth2/v2.1/token';
35
+
36
+ }
37
+
38
+ protected function getUserByToken($token)
39
+ {
40
+ $response = $this->getHttpClient()->get('https://api.line.me/v2/profile', [
41
+ 'headers' => [
42
+ 'X-Line-ChannelToken' => $token,
43
+ ],
44
+ ]);
45
+
46
+ return json_decode($response->getBody(), true);
47
+ }
48
+
49
+ protected function mapUserToObject(array $user)
50
+ {
51
+ return (new User())->setRaw($user)->map([
52
+ 'id' => $user['mid'],
53
+ 'name' => $user['displayName'],
54
+ 'avatar' => $user['pictureUrl'],
55
+ ]);
56
+ }
57
+
58
+ protected function getTokenFields($code)
59
+ {
60
+ return [
61
+ 'client_id' => $this->clientId,
62
+ 'client_secret' => $this->clientSecret,
63
+ 'code' => $code,
64
+ 'redirect_uri' => $this->redirectUrl,
65
+ 'grant_type' => 'authorization_code',
66
+ ];
67
+ }
68
+ ```

1

タイトル変更

2018/06/02 23:56

投稿

FigHy
FigHy

スコア19

title CHANGED
@@ -1,1 +1,1 @@
1
- Laravel Line ログイン
1
+ Laravel Line ログイン認証機能でのエラー
body CHANGED
File without changes