AuthenticateWithDevice initiates the OAuth 2.0 Device Authorization Grant ([RFC 8628]). Display VerificationURI and UserCode to the user, then call [EGS.WaitForDeviceAuthorization] to exchange for an EOS token. [RFC 8628]: https://datatracker.ietf.org/doc/html/rfc8628
()
| 278 | // |
| 279 | // [RFC 8628]: https://datatracker.ietf.org/doc/html/rfc8628 |
| 280 | func (e *EGS) AuthenticateWithDevice() (*DeviceAuthResponse, error) { |
| 281 | req, err := http.NewRequest("POST", "https://api.epicgames.dev/epic/oauth/v2/deviceAuthorization", strings.NewReader("client_id="+eosClientID)) |
| 282 | if err != nil { |
| 283 | return nil, fmt.Errorf("failed to create request: %w", err) |
| 284 | } |
| 285 | |
| 286 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 287 | req.Header.Set("User-Agent", egsUserAgent) |
| 288 | |
| 289 | resp, err := e.client.Do(req) |
| 290 | if err != nil { |
| 291 | return nil, fmt.Errorf("failed to send request: %w", err) |
| 292 | } |
| 293 | defer resp.Body.Close() |
| 294 | |
| 295 | body, err := io.ReadAll(resp.Body) |
| 296 | if err != nil { |
| 297 | return nil, fmt.Errorf("failed to read response: %w", err) |
| 298 | } |
| 299 | |
| 300 | if resp.StatusCode != http.StatusOK { |
| 301 | return nil, fmt.Errorf("unexpected status code %s: %s", resp.Status, string(body)) |
| 302 | } |
| 303 | |
| 304 | var deviceAuthResp DeviceAuthResponse |
| 305 | if err := json.Unmarshal(body, &deviceAuthResp); err != nil { |
| 306 | return nil, fmt.Errorf("failed to parse response: %w", err) |
| 307 | } |
| 308 | |
| 309 | return &deviceAuthResp, nil |
| 310 | } |
| 311 | |
| 312 | // WaitForDeviceAuthorization polls EOS until the user completes authorization at VerificationURI, then returns an EOS token. |
| 313 | func (e *EGS) WaitForDeviceAuthorization(device *DeviceAuthResponse) (*EOSTokenResponse, error) { |