Home > OS >  YouTube v3 API in Unity - OAuth not working? My access tokens are valid, but error says UNAUTHENTICA
YouTube v3 API in Unity - OAuth not working? My access tokens are valid, but error says UNAUTHENTICA

Time:09-24

I have a Unity project making API calls to sites like Patreon and other video streaming websites no problem, but when I use the same method to use YouTube v3 API, it doesn't seem to recognize the access token I am passing to the endpoint.

// MyYouTubeAPI.cs

[Serializable]
struct formData
{
    public string part;
    public string broadcastType;
    public string mine;
    public string key;
}



void Start()
{
    
    StartCoroutine(GetLatestLiveChatID());

}



IEnumerator GetLatestLiveChatID()
{

    // create instance of our class with our values

    formData data = new formData
    {
        part = "snippet,contentDetails,status",
        broadcastType = "all",
        mine = "true",
        key = apiKey
    };
    
    // convert our class to json
    string JsonData = JsonUtility.ToJson(data);

    // instance of unity web request
    UnityWebRequest www = UnityWebRequest.Post("https://youtube.googleapis.com/youtube/v3/liveBroadcasts", "GET");

    // setup upload/download headers (this is what sets the json body)
    byte[] bodyRaw = Encoding.UTF8.GetBytes(JsonData);
    www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
    www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

    // set your headers
    www.SetRequestHeader("Authorization", "Bearer "   accessToken); // MY ACCESS TOKEN ACTUALLY IS VALID THOUGH
    www.SetRequestHeader("Content-Type", "application/json");
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
        Debug.Log(www.downloadHandler.text);  // I GET THESE ERRORS IN UNITY CONSOLE
    }
    else
    {
        // Debug.Log(www.downloadHandler.text);
        jsonString = www.downloadHandler.text;
        Debug.Log(jsonString);
    }


}

However I get the following error inside the Unity console:

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Invalid Credentials",
        "domain": "global",
        "reason": "authError",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"
  }
}

My access token is completely valid, because I get the following when checking its status

{
  "issued_to": [MY CLIENT ID],
  "audience": [MY CLIENT ID],
  "scope": "https://www.googleapis.com/auth/youtube.force-ssl",
  "expires_in": 3586,
  "access_type": "offline"
}

Sorry for my formatting, I don't post much here. Would anyone have any clue why I am running into this problem? I can't for the life of me figure out why my access token isn't being seen or recognized or passed through. But I am at my wits end. Thank you so much for anyone's help!

CodePudding user response:

If you have access to a bash prompt (or Windows command prompt for that matter), then try the following command:

curl "https://youtube.googleapis.com/youtube/v3/liveBroadcasts?part=snippet,contentDetails,status&broadcastType=all&mine=true&access_token=YOUR_ACCESS_TOKEN".

Also try adding the access token to your composed URL: add access_token: accessToken to your formData data and remove the call to www.SetRequestHeader.

I suspect though that your variable accessToken is null or empty.

  • Related