# Accessing API through Unity

**URL:** <https://www.patreondevelopers.com/t/accessing-api-through-unity/563>\
**Category:** Uncategorized\
**Created:** [May 7, 2018, 3:23am UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563 "2018-05-07T03:23:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zornor90](https://avatars.discourse-cdn.com/v4/letter/z/838e76/32.png) [@zornor90](https://www.patreondevelopers.com/u/zornor90)\
**Post date:** [May 7, 2018, 3:23am UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/1 "2018-05-07T03:23:59Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![sam](https://yyz2.discourse-cdn.com/flex036/user_avatar/www.patreondevelopers.com/sam/32/492_2.png) [@sam](https://www.patreondevelopers.com/u/sam)\
**Post date:** [May 7, 2018, 2:36pm UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/2 "2018-05-07T14:36:41Z")

</div>

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).

```bash
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](https://requestbin.net/)):

1. Visit [RequestBin](https://requestbin.net/)
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:

```auto
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`.

---

<div class="post-metadata">

**Author:** ![zornor90](https://avatars.discourse-cdn.com/v4/letter/z/838e76/32.png) [@zornor90](https://www.patreondevelopers.com/u/zornor90)\
**Post date:** [May 7, 2018, 8:37pm UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/3 "2018-05-07T20:37:02Z")

</div>

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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/patreondevelopers/original/1X/34878b1ea62e4cc0387b8ec985713ed108cc38a1.png)

---

<div class="post-metadata">

**Author:** ![zornor90](https://avatars.discourse-cdn.com/v4/letter/z/838e76/32.png) [@zornor90](https://www.patreondevelopers.com/u/zornor90)\
**Post date:** [May 7, 2018, 8:38pm UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/4 "2018-05-07T20:38:20Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/patreondevelopers/original/1X/d54230f81d503fdd929197f9bd5cc90ef844eff2.png)

---

<div class="post-metadata">

**Author:** ![sam](https://yyz2.discourse-cdn.com/flex036/user_avatar/www.patreondevelopers.com/sam/32/492_2.png) [@sam](https://www.patreondevelopers.com/u/sam)\
**Post date:** [May 7, 2018, 9:05pm UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/5 "2018-05-07T21:05:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zornor90](https://avatars.discourse-cdn.com/v4/letter/z/838e76/32.png) [@zornor90](https://www.patreondevelopers.com/u/zornor90)\
**Post date:** [May 7, 2018, 9:19pm UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/6 "2018-05-07T21:19:35Z")

</div>

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):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/patreondevelopers/original/1X/f118f561ccf066c025691c9c25e766d46bf97020.png)

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

---

<div class="post-metadata">

**Author:** ![sam](https://yyz2.discourse-cdn.com/flex036/user_avatar/www.patreondevelopers.com/sam/32/492_2.png) [@sam](https://www.patreondevelopers.com/u/sam)\
**Post date:** [May 7, 2018, 10:01pm UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/7 "2018-05-07T22:01:41Z")

</div>

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

```bash
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](https://www.soscisurvey.de/tools/view-chars.php) to break down the characters in the string.

---

<div class="post-metadata">

**Author:** ![zornor90](https://avatars.discourse-cdn.com/v4/letter/z/838e76/32.png) [@zornor90](https://www.patreondevelopers.com/u/zornor90)\
**Post date:** [May 7, 2018, 10:47pm UTC](https://www.patreondevelopers.com/t/accessing-api-through-unity/563/8 "2018-05-07T22:47:47Z")

</div>

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/](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](https://canada1.discourse-cdn.com/flex036/uploads/patreondevelopers/original/1X/35cb826fb1a7ec63299f5b6c9592b26d729d7ccf.png)

Thanks for your assistance!
