将视频从iOS设备上传到youtube Xamarin iOS

时间:2019-03-05 14:08:26

标签: c# ios xamarin youtube-api youtube-data-api

//我的将凭据分配给youtubeService的代码:

  TokenResponse token = new TokenResponse();

  token.AccessToken = e.User.Authentication.AccessToken;

  token.IssuedUtc = DateTime.Now;

  var time = e.User.Authentication.AccessTokenExpirationDate.SecondsSinceReferenceDate.ToString();  

string[] parts = time.Split('.');

int i1 = int.Parse(parts[0]);

int i2 = int.Parse(parts[1]);

token.ExpiresInSeconds = long.Parse(i1.ToString());

 ClientSecrets clientSecrets = new ClientSecrets();

clientSecrets.ClientId = e.User.Authentication.ClientId;

GoogleAuthorizationCodeFlow.Initializer initializer = new GoogleAuthorizationCodeFlow.Initializer();

initializer.ClientSecrets = clientSecrets;

credential = new UserCredential(new Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow(initializer), e.User.UserID, token);

 youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });

            video.Snippet = new VideoSnippet();
            video.Snippet.Title = "Default Video Title";
            video.Snippet.Description = "Default Video Description";
            video.Snippet.Tags = new string[] { "tag1", "tag2" };
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "private"; // or "private" or "public"
            using (var fileStream = new FileStream(pathString, FileMode.Open))
            {
                var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
                videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
                videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
                 videosInsertRequest.UploadAsync();
            }

//但是我在上传视频时遇到了问题。错误如下:

An error prevented the upload from completing.
System.FormatException: Could not parse value for header 'Content-Range'
  at System.Net.Http.Headers.HttpHeaders.AddInternal (System.String name, System.Collections.Generic.IEnumerable`1[T] values, System.Net.Http.Headers.HeaderInfo headerInfo, System.Boolean ignoreInvalid) [0x00043] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs:196 
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, System.Collections.Generic.IEnumerable`1[T] values) [0x0000e] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs:171 
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, System.String value) [0x00000] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http.Headers/HttpHeaders.cs:163 
  at Google.Apis.Upload.ResumableUpload+<SendNextChunkAsync>d__80.MoveNext () [0x000a7] in C:\Apiary\2019-01-31.11-23-48\Src\Support\Google.Apis\Upload\ResumableUpload.cs:642 
--- End of stack trace from previous location where exception was thrown ---
  at Google.Apis.Upload.ResumableUpload+<UploadCoreAsync>d__77.MoveNext () [0x0005a] in C:\Apiary\2019-01-31.11-23-48\Src\Support\Google.Apis\Upload\ResumableUpload.cs:576 

//可能是在将凭据分配给YouTubeService时发生此错误。让我知道是否有人遇到这种问题。

0 个答案:

没有答案