Accessing API through Unity

Hello, hope you’re doing well! I’m trying to set up the Unity game engine to connect to my campaign to download a list of everyone pledging so that I can set up some custom logic in my application

I’m trying to use WWW to make the GET request as follows:

private IEnumerator GetDataAsync() {
    Dictionary<string, string> headers = new Dictionary<string, string>();
    headers.Add("authorization", "Bearer " + accessToken);
    WWW patreonRequest = new WWW("https://www.patreon.com/api/oauth2/api/current_user", null, headers);

    while (!patreonRequest.isDone) {
      yield return null;
    }
    Debug.Log(patreonRequest.text);
}

However, I keep getting a 401 unauthorized error. I’m using the access token from the Clients & API keys page, and have tried refreshing it several times, but no dice

Thanks!

You can help rule out whether or not the issue has been caused by your code or by the access token by testing the access token’s validity using the following terminal command (replace access-token with your access token).

curl -X GET https://api.patreon.com/oauth2/api/current_user -H 'Authorization: Bearer access-token'

I expect the issue is with your code, presumably the headers are not being set properly. You can debug this by using a Request Bin (like this one):

  1. Visit RequestBin
  2. Create a new RequestBin
  3. Replace the Patreon URL in your code with the RequestBin URL
  4. Run the code so that it makes a request
  5. Refresh the RequestBin webpage: check the data received by the server, if the request was made correctly you should see the following under headers:
Authorization: Bearer access-token

You should keep in mind though that your Creator's Access Token should be kept secret, you should not include that value in anything you deliver to consumers, because they can then extract that token by sniffing traffic and use the token to access the personal details of everybody who is a patron of your campaign. Furthermore, if your access token is reset for some reason (intentionally or accidentally) then the Patreon integration in your game will break.

You should create an intermediary web service that is responsible for talking to the Patreon API, which provides the data your game needs without exposing your Creator's Access Token.

2 Likes

Hey thanks so much for your response!
Totally agree about not passing the Creator Access Token to the finished product, this is just for an editor script I’m writing myself - anything on the client side will use a web service

So I refreshed the access token and tried using curl, and got the same error:

I then tried the RequestBin (really cool tool btw!) from the Unity script, and it does seem to be passing the header correctly although it’s possible the WWW class is not setting a different header that is needed - here is the request with all the headers

image

Looking at the screenshot of your curl request it seems that you may have a new line character in your access token, your access token should be a continuous string with only alphanumeric, dash and underscore characters. Copy your access token into a text editor first and remove any new lines.

1 Like

Thanks for the fast response! I went ahead and tried that and got the same issue, perhaps it appeared that way because I blocked out most of the access token in paint?

Here’s how it appears without the block out (I changed my access token after posting this):

Perhaps there’s some setting I’ve not set correctly or something?

If you look in your screenshot you can see the following:

curl: (6) Could not resolve host: Bearer
curl: (6) Could not resolve host: [...]

This indicates there’s something wrong with your command, because curl is interpreting your command as something other than what we’re intending (a single request to the current_user endpoint).

The issue is either your ' characters are wrong (try replacing them with an actual ' by typing it on your keyboard) or your access token contains invalid characters (almost certainly this). You can use a tool like this one to break down the characters in the string.

1 Like

Ah, you are absolutely right! I got curl working but Unity was still not working. After doing some digging, it turns out that I wasn’t doing anything wrong, but Unity 2017.3.1 is literally just broken when it comes to making web requests…https://forum.unity.com/threads/critical-unity-2017-3-www-with-wwwform-not-working-wrong-network-request-header-body-created.511420/

I tested my script out in Unity 2018.1 and it works perfectly!

image

Thanks for your assistance!