public void OnGetRequestCode() { const string OAUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth"; if (System.IO.File.Exists(TOKEN_FILE)) { return; } var user = OAuthClientInfo.LoadClientSecretsInfo(); var redirectUri = user.RedirectUris.FirstOrDefault(); if (redirectUri == null) { throw new SystemException("Missing redirect url in user credentials"); } var queryParams = new Dictionary <string, string> { { "client_id", user.ClientId }, { "scope", YouTubeService.Scope.YoutubeUpload }, { "response_type", "code" }, { "redirect_uri", redirectUri }, { "access_type", "offline" }, { "prompt", "consent" } }; var newUrl = QueryHelpers.AddQueryString(OAUTH_URL, queryParams); base.Response.Redirect(newUrl); }
private async void ExchangeCodeForTokenAsync(string code) { var user = OAuthClientInfo.LoadClientSecretsInfo(); var redirectUri = user.RedirectUris.FirstOrDefault(); if (string.IsNullOrWhiteSpace(redirectUri)) { throw new SystemException("Missing redirect url in user credentials"); } var payload = new Dictionary <string, string> { { "code", code }, { "client_id", user.ClientId }, { "client_secret", user.ClientSecret }, { "redirect_uri", redirectUri }, { "grant_type", "authorization_code" } }; var content = new FormUrlEncodedContent(payload); var client = _clientFactory.CreateClient(); var response = await client.PostAsync(user.TokenUri, content); response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) { var jsonString = await response.Content.ReadAsStringAsync(); string fileName = System.IO.Path.Combine(_hostingEnvironment.ContentRootPath, TOKEN_FILE); await System.IO.File.WriteAllTextAsync(fileName, jsonString, Encoding.UTF8); } }
public async void OnPostAsync() { var video = GetVideoData(VideoUpload.Title, VideoUpload.Description); var user = OAuthClientInfo.LoadClientSecretsInfo(); if (!System.IO.File.Exists(TOKEN_FILE)) { throw new SystemException("missing access token file. Request for authorization code before uploading a video"); } var tokenResponse = await FetchToken(user); var youTubeService = FetchYouTubeService(tokenResponse, user.ClientId, user.ClientSecret); using (var fileStream = new FileStream(_tempFilePath, FileMode.Open)) { var videosInsertRequest = youTubeService.Videos.Insert(video, "snippet, status", fileStream, "video/*"); videosInsertRequest.ProgressChanged += VideoUploadProgressChanged; videosInsertRequest.ResponseReceived += VideoUploadResponseReceived; await videosInsertRequest.UploadAsync(); } }