I was hoping that someone could help me out here.
I am creating a small Fantasy Football Premier League app using React. There is user specific information that needs authentication before requesting information from the Fantasy Premier League API.
The idea is for the app to start with a login page, where the user inputs their username and password. The app will then post this information to the login URL and return a token, that can be used to retrieve information that requires authentication.
previously, I created the app in python which worked, below was the code:
'login':USERNAME,
'password':PASSWORD,
'redirect_uri': 'https://fantasy.premierleague.com/',
'app':'plfpl-web'
}
s = requests.session()
s.post("https://users.premierleague.com/accounts/login/", data=payload)
myData = s.get('https://fantasy.premierleague.com/api/me').json();
How I know this worked is that when authorized, the variable myData can successfully access 'https://fantasy.premierleague.com/api/me' and gets assigned to all of my authorized information. When my details are incorrect (not authorized), the myData variable is "null" (the values of the keys are of nullType). Using this method, I can request all Authorized information off of the 's' variable.
Now when converting the code to React, I am struggling to successfully Authenticate. I print out the response from a GET call to the "https://fantasy.premierleague.com/api/me" API url and I get the NULL values like I was talking about above. My username and password is correct.
Below is the current code.
formData:any = {
'login': USERNAME,
'password': PASSWORD,
'app': 'plfpl-web',
'redirect_uri': 'https://fantasy.premierleague.com/'
};
login = async (username:string = this.formData.login,password:string = this.formData.password) => {
return axios
.post("https://cors-anywhere.herokuapp.com/https://users.premierleague.com/accounts/login/", this.formData).then((response)=>
{
axios.get("https://cors-anywhere.herokuapp.com/https://fantasy.premierleague.com/api/me")
.then(result => {
console.log(result);
});
});
Please could someone help me out with the following:
- What am I doing wrong and how do I successfully login to FPL via react?
- Once I successfully achieve authentication, I have heard that I may need to retrieve a token number to use in my succeeding calls. How would I do this?
Any help would be greatly appreciated.
Thank you