【问题标题】:Getting unauthorised access in YouTube API [Xamarin.Forms (Portable)]在 YouTube API 中获得未经授权的访问 [Xamarin.Forms (Portable)]
【发布时间】:2017-07-27 18:15:11
【问题描述】:

我正在尝试将视频从 Xamarin.Forms(便携式)上传到 YouTube,我尝试使用 Google APIs,但 Google API 在此阶段与 Xamarin.Forms(便携式)不兼容。所以,我必须通过HttpClient 上传它,但我在StatusCode 中得到Unauthorised

public async Task UploadVideoAsync(Stream stream)
{
    //var token = flow.LoadTokenAsync("", CancellationToken.None).Result;
    string json = @"{
                        ""snippet"": {
                        ""title"": ""using API"",
                        ""description"": ""This is a description of my video"",
                        ""tags"": [""cool"", ""video"", ""more keywords""],
                        ""categoryId"": ""21"",
                        },
                        ""status"": {
                        ""privacyStatus"": ""public"",
                        ""embeddable"": true,
                        ""license"": ""youtube""
                        }
                    }";

    var JsonReqMsg = new StringContent(json);

    JsonReqMsg.Headers.ContentType = new MediaTypeHeaderValue("application/json")
    {
        CharSet = "UTF-8"
    };

    var request = new HttpRequestMessage
        (HttpMethod.Post, new Uri("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status"));
    request.Headers.Add("X-Upload-Content-Length", stream.Length.ToString());
    request.Headers.Add("x-upload-content-type", "video/*");
    request.Content = JsonReqMsg;

    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("Authorization", Constants.API.Google.AccessTokenType + " " + Constants.API.Google.AccessToken);

    var UploadReq = await httpClient.SendAsync(request);
    if (UploadReq.IsSuccessStatusCode)
    {
        IEnumerable<string> _VideoUrl = null;
        var res = await UploadReq.Content.ReadAsStringAsync();
        UploadReq.Headers.TryGetValues("Location", out _VideoUrl);

        var binaryContent = new StreamContent(stream);
        var UploadReq_ = await httpClient.PutAsync(new Uri(_VideoUrl.ToString()), binaryContent);
        if (UploadReq_.IsSuccessStatusCode)
        {
            var res_ = await UploadReq_.Content.ReadAsStringAsync();
        }
    }
}

代码有问题吗?

【问题讨论】:

  • 尚未使用该 API,但到目前为止,似乎没有指定授权信息。既不在json 变量中,也不在request
  • 那里有 DefaultRequestHeaders

标签: c# youtube xamarin.forms youtube-data-api unauthorized


【解决方案1】:

您的授权不正确。您应该使用“承载”或开发人员密钥。这是 YouTube 文档:https://developers.google.com/youtube/2.0/developers_guide_protocol#OAuth2_Calling_a_Google_API

【讨论】:

  • 我正在传递“Bearer”,我正在从登录 API 获取 AccessTokenType。 Constants.API.Google.AccessTokenType + " " + Constants.API.Google.AccessToken
猜你喜欢
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 2017-02-26
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 2012-01-03
相关资源
最近更新 更多