t3jem
t3jem

Reputation: 53

Authorization header not getting passed via UnityWebRequest

I'm trying to pass an OAuth2 bearer token along with a Post request to my server (Using the Google Apps Script Executions API). I keep getting a 401 unauthorized error any way I try to set the Authorization token. I'm sure I'm setting it wrong somehow, but I've been unable to figure out what's wrong.

Below is my current code.

private static UnityWebRequest createRequest(string functionName, List<string> parameters)
{
    PostData data = new PostData();
    data.Add("function", functionName);
    data.Add("parameters", string.Join(",", parameters.ToArray()));
    data.Add("devMode", "true"); // TODO: remove before launch

    Debug.Log(data.toJsonString());
    UnityWebRequest request = new UnityWebRequest(
            "https://script.googleapis.com/v1/scripts/" + SERVER_SCRIPT_ID + ":run",
            UnityWebRequest.kHttpVerbPOST);
    UploadHandlerRaw uploadHandler = new UploadHandlerRaw(data.toJsonBytes());
    request.uploadHandler = uploadHandler;
    request.downloadHandler = new DownloadHandlerBuffer();
    request.SetRequestHeader("Content-Type", "application/json; charset=utf-8");
    request.SetRequestHeader("Authorization", "Bearer " + DriveAPI.getInstance().getAuthToken());

    yield return request.Send();
    Debug.Log(request.downloadHandler.text);
}

[Serializable]
private class PostData : Dictionary<string, string>
{
    public byte[] toJsonBytes()
    {
        return Encoding.ASCII.GetBytes(toJsonString());
    }
    public string toJsonString()
    {
        string result = "{";
        foreach (string key in this.Keys)
        {
            string value;
            TryGetValue(key, out value);
            result += "\"" + key + "\":\"" + value + "\",";
        }
        result = result.Substring(0, result.Length - 1) + "}";
        return result;
    }
}

I have tried setting the headers in the actual Data object, but didn't have luck there either.

Upvotes: 3

Views: 13295

Answers (2)

t3jem
t3jem

Reputation: 53

Turns out my code was working fine.

The error message from Apps Script is misleading as the Auth token was being sent. I was able to test correctness by generating an auth token from my script (that's serving the Execution API endpoints) and hard-coding that token in my app. Doing so resulted in a successful request.

I've checked that the cloud projects used in the script and my app are the same, but am still receiving the error, but that's a problem for another question :).

Upvotes: 2

joreldraw
joreldraw

Reputation: 1736

Try enconding to UTF8, changing UploadHandleRaw to UploadHandle and setting request to single Post:

private static UnityWebRequest createRequest(string functionName, List<string> parameters)
{
    PostData data = new PostData();
    data.Add("function", functionName);
    data.Add("parameters", string.Join(",", parameters.ToArray()));
    data.Add("devMode", "true"); // TODO: remove before launch

    Debug.Log(data.toJsonString());
    UnityWebRequest request = new UnityWebRequest("https://script.googleapis.com/v1/scripts/" + SERVER_SCRIPT_ID + ":run", "POST");
    UploadHandler uploadHandler =(UploadHandler) new UploadHandlerRaw(Encoding.UTF8.GetBytes(data.toJsonString()));
    request.uploadHandler = uploadHandler;
    request.downloadHandler = new DownloadHandlerBuffer();
    request.SetRequestHeader("Content-Type", "application/json");
    request.SetRequestHeader("Authorization", "Bearer " + DriveAPI.getInstance().getAuthToken());

    yield return request.Send();

    if (www.error != null)
    {
        Debug.Log("Error: " + www.error);
    }
    else
    {
        Debug.Log("Status Code: " + request.responseCode);
    }

}

Upvotes: 0

Related Questions