3

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

To bring this full circle. I was finally able to generate a good Auth token by ensuring I was using the scopes required by the script when generating my auth token (I was using Drive.file scope when I needed the full drive scope and wasn't including script properties).
0

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);
    }

}

1 Comment

No luck unfortunately. After some more debugging I actually found this to be an issue with the auth token itself rather than the request (the error message is terribly misleading). Generating an auth token from my script and hard-coding that into the request causes the request to succeed. So my issue is with the auth code being generated rather than the request itself. (I did ensure I'm using the same cloud project, so I'm still investigating that issue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.