2017-08-22 6 views
0

私はドライブAPIにアクセスするために以下のコードを使用しています。そのためには、MVC WebアプリケーションではなくWEBAPIを使用しています。リダイレクト中にGoogleドライブの承認コードWEBAPI

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は難しいので、承認されたコードの結果を一度管理するにはどうすればよいですか。資格情報に入力されませんか?

コードを承認する他の方法はありますか?

答えて

0

質問にお答えするのはdocumentationです。

OAuth 2.0の

この文書では、 クライアントIDを取得する方法、それを使用する際に、OAuth 2.0のを説明し、そしてどのように .NET用Google APIクライアントライブラリでそれを使用します。

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(); 
      ... 
     } 
    } 
} 
関連する問題