public Client(IHttpClientFactory clientFactory, IRequestGenerator requestGenerator, Options options)
        {
            _options = options;

            _clientOAuth = clientFactory.CreateHttpClient(new HttpClientOptions());
            _clientContent = clientFactory.CreateHttpClient(new HttpClientOptions {AddTokenToRequests = true, TokenRetriever = () => _options.AccessToken});
            _clientContentNoRedirection = clientFactory.CreateHttpClient(new HttpClientOptions {AllowAutoRedirect = false, AddTokenToRequests = true, TokenRetriever = () => _options.AccessToken});


            RequestGenerator = requestGenerator;
        }
Exemple #2
0
        public static async Task Run()
        {
            var options = new Options
                {
                    ClientId = "...",
                    ClientSecret = "...",
                    CallbackUrl = "https://login.live.com/oauth20_desktop.srf",

                    AutoRefreshTokens = true,
                    PrettyJson = false,
                    ReadRequestsPerSecond = 2,
                    WriteRequestsPerSecond = 2
                };

            // Initialize a new Client (without an Access/Refresh tokens
            var client = new Client(options);

            // Get the OAuth Request Url
            var authRequestUrl = client.GetAuthorizationRequestUrl(new[] {Scope.Basic, Scope.Signin, Scope.SkyDrive, Scope.SkyDriveUpdate});

            // Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
            var authCode = await GetAuthCode(client, new[] { Scope.Signin, Scope.Basic, Scope.SkyDrive });

            // Exchange the Authorization Code with Access/Refresh tokens
            var token = await client.GetAccessTokenAsync(authCode);

            // Get user profile
            var userProfile = await client.GetMeAsync();
            Console.WriteLine("Name: " + userProfile.Name);
            string preferredEmail = userProfile.Emails == null ? "(n/a)" : userProfile.Emails.Preferred;
            Console.WriteLine("Preferred Email: " + preferredEmail);

            // Get user photo
            var userProfilePicture = await client.GetProfilePictureAsync(PictureSize.Small);
            Console.WriteLine("Avatar: " + userProfilePicture);

            // Retrieve the root folder
            var rootFolder = await client.GetFolderAsync();
            Console.WriteLine("Root Folder: {0} (Id: {1})", rootFolder.Name, rootFolder.Id);

            // Retrieve the content of the root folder
            var folderContent = await client.GetContentsAsync(rootFolder.Id);
            foreach (var item in folderContent)
            {
                Console.WriteLine("\tItem ({0}: {1} (Id: {2})", item.Type, item.Name, item.Id);
            }


            // Initialize a new Client, this time by providing previously requested Access/Refresh tokens
            options.AccessToken = token.Access_Token;
            options.RefreshToken = token.Refresh_Token;
            var client2 = new Client(options);

            // Search for a file by pattern (e.g. *.docx for MS Word documents)
            //TODO: Uncomment the below when PR #5 is merged
            //var wordDocuments = await client.SearchAsync("*.docx");
            //Console.WriteLine(string.Format("Found {0} Word documents", wordDocuments.Count()));

            // Find a file in the root folder
            var file = folderContent.FirstOrDefault(x => x.Type == File.FileType);

            // Download file to a temporary local file
            var tempFile = Path.GetTempFileName();
            using (var fileStream = System.IO.File.OpenWrite(tempFile))
            {
                var contentStream = await client2.DownloadAsync(file.Id);
                await contentStream.CopyToAsync(fileStream);
            }


            // Upload the file with a new name
            using (var fileStream = System.IO.File.OpenRead(tempFile))
            {
                await client2.UploadAsync(rootFolder.Id, fileStream, "Copy Of " + file.Name);
            }

        }
 public Client(Options options)
     : this(new HttpClientFactory(), new RequestGenerator(), options)
 { }
        public  async Task<Options> GetAuthorizationToken()
        {
            Options options = new Options
            {
                ClientId = "0000000048152A56",
                ClientSecret = "PhQ7Z62wSxw6OvwvCfDWG2zKHlbLUCSt",
                CallbackUrl = "https://login.live.com/oauth20_desktop.srf",

                AutoRefreshTokens = true,
                PrettyJson = false,
                ReadRequestsPerSecond = 2,
                WriteRequestsPerSecond = 2
            };

            // Initialize a new Client (without an Access/Refresh tokens
            Client client = new Client(options);

            // Get the OAuth Request Url
            // var authRequestUrl = client.GetAuthorizationRequestUrl(new[] { Scope.Basic, Scope.Signin, Scope.SkyDrive, Scope.SkyDriveUpdate });

            // Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
            string authCode = await GetAuthCode(client, new[] { Scope.Signin, Scope.Basic, Scope.SkyDrive, Scope.SkyDriveUpdate, Scope.Photos });

            // Exchange the Authorization Code with Access/Refresh tokens
            UserToken token = await client.GetAccessTokenAsync(authCode);

            options.AccessToken = token.Access_Token;
            options.RefreshToken = token.Refresh_Token;

            //var auth=await client.GetAccessTokenAsync()
            // Get user profile
            User userProfile = await client.GetMeAsync();
            Debug.WriteLine("Name: " + userProfile.Name);
            return options;

        }