Skip to content

Instantly share code, notes, and snippets.

@jack3898
Last active April 17, 2024 10:04
Show Gist options
  • Save jack3898/3b96e05c5ee11e00877f85378bd1d14c to your computer and use it in GitHub Desktop.
Save jack3898/3b96e05c5ee11e00877f85378bd1d14c to your computer and use it in GitHub Desktop.
How to authenticate with FakeYou using a username and password

How to authenticate with the FakeYou API without an API key

FakeYou's API does not have public support for an API key so unless you have been given an API token by the owner, you're left with session authentication.

This guide will predominantly revolve around TypeScript, but in the end, we will be using HTTP as our transfer protocol. So long as your language works with HTTP, you should be able to translate it to your language of choice!

Understanding how to authenticate

We will use session authentication, which means you need to send across a cookie to the API so that FakeYou recognises you.

That's it!

You will basically be replicating the exact technique the web browser uses when you log in to the official FakeYou platform.

Step 1, get the cookie 🍪

You first need the cookie to authenticate! But how do we retrieve it?

You need to send a POST request to https://api.fakeyou.com/v1/login. The body will be the login credentials.

const response = await fetch("https://api.fakeyou.com/v1/login", {
    method: "POST",
    body: JSON.stringify({
        username_or_email: usernameOrEmail,
        password: password,
    }),
});

const json: { success: boolean } = await response.json();

Then check it was successful:

if (!json.success) {
    throw new Error("Authentication failed.");
}

And extract the cookie as a string:

const cookie = response.headers
    .get("set-cookie")
    ?.match(/^\w+.=([^;]+)/)
    ?.at(1);

The above is a regular expression that extracts the cookie from the response headers. It's worked well for fakeyou.ts! Maybe you could simplify it.

Now just keep that cookie in your application ready subsequent requests.

Step 2, send the cookie 🚀🍪

Now you have the cookie, you should now include it in every subsequent request to authenticate.

const headers = new Headers();

headers.append("content-type", "application/json");
headers.append("credentials", "include"); // IMPORTANT! Your cookie will not be sent without this!
headers.append("cookie", `session=${cookie}`); // Add the cookie

const response = await fetch("https://api.fakeyou.com/endpoint", {
    headers,
    ...options, // More options for the fetch
});

const json = await response.json();

Now, keep in mind the above example does not use a valid URL. Do check out the FakeYou docs for more information about what URLs you can leverage for your own TTS application.

Cookie expiry?

As should be expected, your cookie will expire at some point in the future.

Your cookie will expire in exactly 7300 days (or just under 10 years).

So just make sure your cookie is never leaked. And use the /logout endpoint to logout so that the cookie can no longer be used.

And that's a wrap!

In summary, extract the cookie using the /v1/login endpoint, then add it as a header for subsequent requests.

Links

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