| 72 | } |
| 73 | |
| 74 | async getAccessTokenFromCode(authData) { |
| 75 | if (!authData.code) { |
| 76 | throw new Parse.Error( |
| 77 | Parse.Error.OBJECT_NOT_FOUND, |
| 78 | 'Line auth is invalid for this user.' |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | const tokenUrl = 'https://api.line.me/oauth2/v2.1/token'; |
| 83 | const response = await fetch(tokenUrl, { |
| 84 | method: 'POST', |
| 85 | headers: { |
| 86 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 87 | }, |
| 88 | body: new URLSearchParams({ |
| 89 | client_id: this.clientId, |
| 90 | client_secret: this.clientSecret, |
| 91 | grant_type: 'authorization_code', |
| 92 | redirect_uri: authData.redirect_uri, |
| 93 | code: authData.code, |
| 94 | }), |
| 95 | }); |
| 96 | |
| 97 | if (!response.ok) { |
| 98 | throw new Parse.Error( |
| 99 | Parse.Error.OBJECT_NOT_FOUND, |
| 100 | `Failed to exchange code for token: ${response.statusText}` |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | const data = await response.json(); |
| 105 | if (data.error) { |
| 106 | throw new Parse.Error( |
| 107 | Parse.Error.OBJECT_NOT_FOUND, |
| 108 | data.error_description || data.error |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | return data.access_token; |
| 113 | } |
| 114 | |
| 115 | async getUserFromAccessToken(accessToken) { |
| 116 | const userApiUrl = 'https://api.line.me/v2/profile'; |