Пример #1
0
        public static void AddCommmonHeaders(this WebClient request, SharedAccessSignatureTokenProvider provider, string address, bool addContentType = true, bool addAnonHeader = false, bool addIfMatchheader = false, string qaddress = null)
        {
            if (addContentType)
            {
                request.AddContentType();
            }
            if (addAnonHeader)
            {
                request.Headers.Add("X-MS-ISANONYMOUSACCESSIBLE", "False");
            }
            if (addIfMatchheader)
            {
                request.Headers.Add("If-Match", "*");
            }
            request.SetUserAgentHeader();
            request.AddXProcessAtHeader();
            request.AddAuthorizationHeader(provider, address);

            if (!string.IsNullOrWhiteSpace(qaddress))
            {
                request.AddServiceBusSupplementaryAuthorizationHeader(provider, qaddress);
            }

            request.AddTrackingIdHeader(Guid.NewGuid());
        }
Пример #2
0
        public void AddAuthorizationHeader_throws_on_null_arguments()
        {
            var token       = JsonConvert.DeserializeObject(@"{ ""access_token"": ""access_token"", ""token_type"": ""token_type"", ""expires_in"": 130.5 }");
            var accessToken = new JsonWebTokenAccessToken(token);

            Assert.Throws <ArgumentNullException>(() => ((null) as WebClient).AddAuthorizationHeader(accessToken));
            using (var webClient = new WebClient())
            {
                Assert.Throws <ArgumentNullException>(() => webClient.AddAuthorizationHeader(null));
            }
        }
Пример #3
0
        /// <summary>
        /// Downloads the specified file as CSV.
        /// </summary>
        /// <param name="serviceAccount">The service account.</param>
        /// <param name="certificate">The certificate.</param>
        /// <param name="fileId">The file identifier.</param>
        /// <param name="destinationFilePath">The destination file path.</param>
        /// <returns>
        /// Task tracking asynchronous processes.
        /// </returns>
        /// <exception cref="ArgumentNullException">If any arguments are null or empty.</exception>
        /// <exception cref="WebException">If Unable to download file or file does not contain an export link for csv.</exception>
        public static async Task DownloadAsCsv(string serviceAccount, X509Certificate2 certificate, string fileId, string destinationFilePath)
        {
            if (string.IsNullOrWhiteSpace(serviceAccount))
            {
                throw new ArgumentNullException("serviceAccount");
            }

            if (certificate == null)
            {
                throw new ArgumentNullException("certificate");
            }

            if (string.IsNullOrWhiteSpace(fileId))
            {
                throw new ArgumentNullException("fileId");
            }

            if (string.IsNullOrWhiteSpace(destinationFilePath))
            {
                throw new ArgumentNullException("destinationFilePath");
            }

            var accessToken = await GoogleAuthorizer.Authorize(serviceAccount, certificate, DriveReadOnlyScope);

            var webClient = new WebClient();

            webClient.AddAuthorizationHeader(accessToken);

            var response = await webClient.DownloadStringTaskAsync(FilesApiBaseUri + fileId);

            var file = JsonWebToken.JsonWebTokenJsonDeserialize(response);

            if (file == null)
            {
                throw new WebException($"Unable to download file '{fileId}'");
            }

            var csvExportLink = ((dynamic)file).exportLinks["text/csv"];

            if (csvExportLink == null || string.IsNullOrWhiteSpace(csvExportLink.ToString()))
            {
                throw new WebException("${fileId} did not contain an export link for csv");
            }

            var csvExportUri = new Uri(csvExportLink.ToString());

            var webClient2 = new WebClient();

            webClient2.AddAuthorizationHeader(accessToken);

            await webClient2.DownloadFileTaskAsync(csvExportUri, destinationFilePath);
        }
Пример #4
0
        public void Constructor_reads_access_token_and_sets_properties()
        {
            var token       = JsonConvert.DeserializeObject(@"{ ""access_token"": ""access_token"", ""token_type"": ""token_type"", ""expires_in"": 130.5 }");
            var accessToken = new JsonWebTokenAccessToken(token);

            string authorizationHeader;

            using (var webClient = new WebClient())
            {
                webClient.AddAuthorizationHeader(accessToken);

                authorizationHeader = webClient.Headers[HttpRequestHeader.Authorization];
            }

            Assert.That(authorizationHeader, Is.Not.Null);
            Assert.That(authorizationHeader, Is.EqualTo("token_type access_token"));
        }