Skip to content

Instantly share code, notes, and snippets.

@pirica
Forked from MixV2/auth.js
Created July 24, 2021 11:01
Show Gist options
  • Save pirica/95cd58c4a9a4158ba5290534ad6ee12b to your computer and use it in GitHub Desktop.
Save pirica/95cd58c4a9a4158ba5290534ad6ee12b to your computer and use it in GitHub Desktop.
Authenticating with Epic Games
// by joe/mix
const request = require("request-promise");
module.exports = {
/**
* Login to an account.
* @param {string} email
* @param {string} password
*/
async authenticate(email, password) {
let jar = await request.jar();
await this.sendEpicRequest("https://www.epicgames.com/id/api/reputation", jar);
await this.sendEpicRequest("https://www.epicgames.com/id/api/authenticate", jar);
await this.sendEpicRequest("https://www.epicgames.com/id/api/location", jar);
const csrf = await this.sendEpicRequest("https://www.epicgames.com/id/api/csrf", jar);
const cookies = JSON.parse(JSON.stringify(jar))._jar["cookies"];
const login = await request({
url: "https://www.epicgames.com/id/api/login",
method: "POST",
headers: {
"x-xsrf-token": cookies.find(i => i.key === "XSRF-TOKEN").value,
"Content-Type": "application/json",
},
jar: jar,
form: {
"email": email,
"password": password,
"rememberMe": true
}
});
const exchange = await request({
url: "https://www.epicgames.com/id/api/exchange",
headers: {
"x-xsrf-token": cookies.find(i => i.key === "XSRF-TOKEN").value,
"Content-Type": "application/json",
},
jar: jar,
json: true,
});
const token = await request({
url: "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/token",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "basic ZWM2ODRiOGM2ODdmNDc5ZmFkZWEzY2IyYWQ4M2Y1YzY6ZTFmMzFjMjExZjI4NDEzMTg2MjYyZDM3YTEzZmM4NGQ=" // https://github.com/MixV2/EpicResearch#1231-authorization-header
},
form: {
grant_type: "exchange_code",
exchange_code: exchange["code"],
includePerms: false
},
json: true
})
return token;
},
/**
* Sends an "Epic" request, reuses cookie jar and shortens to one line.
* @param {string} url
*/
async sendEpicRequest(url, jar) {
return await request({
url,
jar
})
}
}
const auth = require("./auth");
/**
* An async function that authenticates and logs the account token.
*/
( async () => {
const login = await auth.authenticate("[email protected]", "GreatPassword");
console.log(login);
})();
@djjddhdvdys
Copy link

Login

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment