Expo SDK Version: 37.0.3Platforms(Android/iOS/web/all): iOS
Hello, I am trying to build a fantasy basketball app based on the Yahoo Fantasy API. I am trying to use AuthSession API from expo in order to do the OAuth Authentication process listed here. I am able to get the Authorization code from yahoo after the user authenticates using AuthSession, I then need to take that code and send it to the OAuth 2.0 Token Endpoint: using the HTTP Post method. I send the data using fetch and I am getting the error below back:
{ "error": "INVALID_AUTHORIZATION_CODE","error_description": "OAuth authorization code expired or invalid",}
I've pasted my code for the signIn() function below. Please let me know if you have any pointers as to how I can clear this error and get the Access token.
import React, { useEffect, useState } from 'react';import {View, Text, TouchableOpacity, Alert } from 'react-native';import * as WebBrowser from 'expo-web-browser';import { makeRedirectUri, useAuthRequest } from 'expo-auth-session';import { Base64 } from 'js-base64';const useProxy = true;const redirectUri = makeRedirectUri({ useProxy });const CLIENT_ID = *scrambledcleintid*const CLIENT_SECRET = *scrambledclientsecret*function SignIn({ navigation }) { WebBrowser.maybeCompleteAuthSession(); var code; const discovery = { authorizationEndpoint: 'https://api.login.yahoo.com/oauth2/request_auth', tokenEndpoint: 'https://api.login.yahoo.com/oauth2/get_token' };//request const [request, result, promptAsync] = useAuthRequest({ clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, scopes: ["openid"], responseType:'code', redirectUri, //will likely need to change for production extraParams: { // ideally, this will be a random value nonce: "nonce", }, },discovery);useEffect(() => {if (result) { if (result.error) { Alert.alert("Authentication error", result.params.error_description || "something went wrong" ); return; } if (result.type === "success") { code = result.params.code; getTokens(); //const { name } = code; //setName(name); } } }, [result]); const getTokens = async () => { try { const authcode = Base64.encode(`${CLIENT_ID}:${CLIENT_SECRET}`); const bodystr = `code=${code}&grant_type=authorization_code&redirect_uri=${redirectUri}`; const response = await fetch('https://api.login.yahoo.com/oauth2/get_token', { method: 'POST', headers: { Authorization : `Basic ${authcode}`,'Content-Type' : 'application/x-www-form-urlencoded', }, body: bodystr }); const responseJson = await response.json(); } catch (err) { console.error(err); } } return (<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}><Text>Sign In screen</Text><TouchableOpacity style = {styles.button} onPress={() => promptAsync({ useProxy, redirectUri })}><Text style={styles.buttonText}>Sign In</Text> </TouchableOpacity></View> );}export default SignIn;