Search code examples
c#youtubeyoutube-apigoogle-oauth

Why is Youtube's v3 api claiming my access token does not have permissions to insert live streams?


In the authorization stage of my application I'm requesting access via:

  var req = new Google.Apis.Auth.OAuth2.Requests.GoogleAuthorizationCodeRequestUrl(new Uri(string.Format(Settings.Google.OAuth.Url, "auth")));
  req.ClientId = Settings.Google.OAuth.ClientId;
  req.ResponseType = "code";
  req.Scope = "https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtubepartner";
  req.RedirectUri = string.Format(Settings.Integration.HandshakeUrl, "youtube");
  req.AccessType = "offline"; // required to get refreshToken.
  req.ApprovalPrompt = "force";

  req.State = Application.Cryptography.Encrypt(Application.JSON.SerializeToString<State>(new State { UID = userId, PROFILEID = profileId, SUCCESS = request.SuccessUrl, FAIL = request.FailUrl }), Settings.Cryptography.SymetricKey);

  // Return the url that the requesting application should redirect to in order to perform the authorization.
  return req.Build().ToString();

This successfully gets me an access token and refresh token. Now I wanted to insert a new stream based on the information in the google api docs

  var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse { RefreshToken = refreshToken, AccessToken = accessToken };
  var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
  {
    ClientSecrets = new ClientSecrets
    {
      ClientId = "<id>",
      ClientSecret = "<secret>",
    }
  }), string.Empty, token);

  var service = new YouTubeService(new BaseClientService.Initializer
  {
    HttpClientInitializer = credentials
  });

  var streamResource = new LiveStreamsResource(service);
  var result = streamResource.Insert(new LiveStream
  {
    Snippet = new LiveStreamSnippet
    {
      Title = "Stream"
    },

    Cdn = new CdnSettings
    {
      Format = "1080p",
      IngestionType = "rtmp"
    }
  }, "id, snippet, cdn, status");

  var returnedStream = result.Execute();

When this runs Execute() gives the following exception:

Google.Apis.Requests.RequestError

Request is not authorized [403]

Errors [

    Message[Request is not authorized] Location[ - ] Reason[insufficientLivePermissions] Domain[youtube.liveStream]

]

I can't figure out what I'm doing wrong in this process. Even the API explorer


Solution

  • Apparently, I was looking at this all wrong (and Google's API documentation should really describe this).

    The response isn't that I don't have access to the proper scopes, it's that even though I was authorizing myself for the youtube scope, my youtube account did not have live streams enabled (separate option).

    After enabling live streaming on my account this code worked properly.