重定向网址WEBAPI时的Google云端硬盘授权代码

时间:2017-08-22 11:51:50

标签: asp.net-web-api google-drive-api

我正在使用Below Code访问Drive API,因为我使用的是WEBAPI而不是MVC Web应用程序。

var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = new ClientSecrets
    {
        ClientId = "XXXXX",
        ClientSecret = "XXXXX"
    },
    Scopes = new[] { DriveService.Scope.Drive }
});

//  var result = new AuthorizationCodeWebApp(flow, "http://localhost:1344/api/AuthCallback/IndexAsync", "");
var result = new AuthorizationCodeWebApp(flow, "http://localhost:1344/api/GoogleImport/Listfile", "");
var resultData = result.AuthorizeAsync("user",CancellationToken.None).Result;
if (result1.Credential != null)
{
    var service123 = new DriveService(new BaseClientService.Initializer
    {
        HttpClientInitializer = resultData.Credential,
        ApplicationName = "ASP.NET MVC Sample",
    });

    var list = await service123.Files.List().ExecuteAsync();
}
else
{
    System.Web.HttpContext.Current.Response.Redirect(resultData.RedirectUri);
}

在webapi中,Response.Redirect很难,所以如何管理授权代码结果。凭证没有填充?

还有其他方法可以授权代码吗?

1 个答案:

答案 0 :(得分:0)

以下documentation可以帮助您解决问题。

OAuth 2.0

  

本文档介绍了OAuth 2.0,何时使用它,如何获取   客户端ID,以及如何将其与Google API客户端库配合使用   .NET。

OAuth 2.0协议

  

OAuth 2.0是Google API使用的授权协议。您   应通过阅读以下链接熟悉协议:

     

还提供了有关如何实施OAuth 2.0的示例代码。试着检查一下。您可以在代码上执行相同的操作。

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Books.v1;
using Google.Apis.Books.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;

namespace Books.ListMyLibrary
{
    /// <summary>
    /// Sample which demonstrates how to use the Books API.
    /// https://developers.google.com/books/docs/v1/getting_started
    /// <summary>
    internal class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("Books API Sample: List MyLibrary");
            Console.WriteLine("================================");
            try
            {
                new Program().Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine("ERROR: " + e.Message);
                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        private async Task Run()
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { BooksService.Scope.Books },
                    "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
            }

            // Create the service.
            var service = new BooksService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Books API Sample",
                });

            var bookshelves = await service.Mylibrary.Bookshelves.List().ExecuteAsync();
            ...
        }
    }
}