• <tfoot id='rAKm9'></tfoot>
        • <bdo id='rAKm9'></bdo><ul id='rAKm9'></ul>

      1. <legend id='rAKm9'><style id='rAKm9'><dir id='rAKm9'><q id='rAKm9'></q></dir></style></legend>
      2. <small id='rAKm9'></small><noframes id='rAKm9'>

        <i id='rAKm9'><tr id='rAKm9'><dt id='rAKm9'><q id='rAKm9'><span id='rAKm9'><b id='rAKm9'><form id='rAKm9'><ins id='rAKm9'></ins><ul id='rAKm9'></ul><sub id='rAKm9'></sub></form><legend id='rAKm9'></legend><bdo id='rAKm9'><pre id='rAKm9'><center id='rAKm9'></center></pre></bdo></b><th id='rAKm9'></th></span></q></dt></tr></i><div id='rAKm9'><tfoot id='rAKm9'></tfoot><dl id='rAKm9'><fieldset id='rAKm9'></fieldset></dl></div>

        MVC 中的 GoogleWebAuthorizationBroker 用于 Google Drive 访

        时间:2023-08-28
          <tbody id='EFbVA'></tbody>
        1. <tfoot id='EFbVA'></tfoot>
            <bdo id='EFbVA'></bdo><ul id='EFbVA'></ul>

                <i id='EFbVA'><tr id='EFbVA'><dt id='EFbVA'><q id='EFbVA'><span id='EFbVA'><b id='EFbVA'><form id='EFbVA'><ins id='EFbVA'></ins><ul id='EFbVA'></ul><sub id='EFbVA'></sub></form><legend id='EFbVA'></legend><bdo id='EFbVA'><pre id='EFbVA'><center id='EFbVA'></center></pre></bdo></b><th id='EFbVA'></th></span></q></dt></tr></i><div id='EFbVA'><tfoot id='EFbVA'></tfoot><dl id='EFbVA'><fieldset id='EFbVA'></fieldset></dl></div>
              • <legend id='EFbVA'><style id='EFbVA'><dir id='EFbVA'><q id='EFbVA'></q></dir></style></legend>

                  <small id='EFbVA'></small><noframes id='EFbVA'>

                  本文介绍了MVC 中的 GoogleWebAuthorizationBroker 用于 Google Drive 访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我无法从 MVC 应用程序访问特定的 Google Drive 帐户.我所需要的只是让 MVC Web 应用程序访问我的谷歌驱动器扫描几个文件并根据谷歌驱动器的内容更改数据库.问题是在 IIS 中运行时,驱动器无法通过身份验证,因为如果 GoogleWebAuthorizationBroker 尝试打开浏览器(如果它是 Windows 应用程序)但似乎无法通过 IIS 执行此操作,即使它是服务器端的.

                  I'm stuck trying to access a specific Google drive account from a MVC app. All I need is for the MVC web app to access my google drive scan for a few files and alter the database based on the contents of the google drive. The problem is when running in IIS the drive cannot be authenticated as GoogleWebAuthorizationBroker tries to open browser if its a windows app but doesn't seem to be able to do that through IIS and even if it did it would be server side.

                  理想情况下,我根本不需要对这个应用程序进行身份验证,但如果它确实通过了验证,那么我该如何让它在 IIS 中工作?

                  Ideally I would not have to authenticate this app at all, but if it has do go through that then how do I make it work in IIS?

                  UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                      new ClientSecrets
                              {
                                  ClientId = "MY_ID",
                                  ClientSecret = "My_Secret"
                              },
                              new[] { DriveService.Scope.Drive },
                              "user",
                              CancellationToken.None, dataStore: new FileDataStore(Server.MapPath("~/app_data/googledata"))).Result;
                  

                  推荐答案

                  我得到了这个工作,能够使网站使用我的帐户访问 Google 驱动器,而无需要求用户登录或授权.

                  I got this to work, was able to enable the web site to access Google drive using my account without asking users to login or authorize.

                  首先,点击此链接让 Google API 与 MVC 一起工作:

                  First of all, follow this link to get Google API work with MVC:

                  https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web_applications

                  示例代码中存在问题,在 HomeController 中

                  There is a problem in the Sample code, in HomeController

                   public async Task IndexAsync(CancellationToken cancellationToken)
                  

                  应该是:

                   public async Task<ActionResult> IndexAsync(CancellationToken cancellationToken)
                  

                  之后,我创建了一个 MemoryDataStore(参见最后的代码),它是对此处发布的 MemoryDataStore 的轻微修改:

                  After that, I created a MemoryDataStore (see code at the end) that is a slightly modification from the MemoryDataStore posted here:

                  http://conficient.wordpress.com/2014/06/18/using-google-drive-api-with-c-part-2/

                  完成此操作后,捕获您正在使用的帐户的刷新令牌,并在验证时将存储替换为此存储:

                  Once you do that, capture the refresh token of the account you are using, and replace the store with this store when authenticate:

                      private static readonly IAuthorizationCodeFlow flow =
                          new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                              {
                                  ClientSecrets = new ClientSecrets
                                  {
                                      ClientId = clientID,
                                      ClientSecret = clientSecret
                                  },
                                  Scopes = new[] { DriveService.Scope.Drive },
                                  //DataStore = new FileDataStore("Drive.Api.Auth.Store")
                                  DataStore = new GDriveMemoryDataStore(commonUser, refreshToken)
                              });
                  

                  这里的 commonUser 是您选择的预定义用户 ID.请务必修改 GetUserID() 方法以返回相同的 commonUser:

                  Here commonUser is a predefined user id of your chosen. Please make sure to modify the GetUserID() method to return the same commonUser:

                   public override string GetUserId(Controller controller)
                      {
                          return commonUser;
                      }
                  

                  完成此操作后,Google Drive 将停止要求用户登录和授权应用程序.

                  Once this is done, Google drive will stop asking user to login and authorize the app.

                  这是我的 MemoryDataStore 代码:

                  Here is my MemoryDataStore code:

                   /// <summary>
                   /// Handles internal token storage, bypassing filesystem
                   /// </summary>
                  internal class GDriveMemoryDataStore : IDataStore
                   {
                       private Dictionary<string, TokenResponse> _store;
                       private Dictionary<string, string> _stringStore;
                  
                       //private key password: notasecret
                  
                       public GDriveMemoryDataStore()
                       {
                           _store = new Dictionary<string, TokenResponse>();
                           _stringStore = new Dictionary<string, string>();
                       }
                  
                       public GDriveMemoryDataStore(string key, string refreshToken)
                       {
                           if (string.IsNullOrEmpty(key))
                               throw new ArgumentNullException("key");
                           if (string.IsNullOrEmpty(refreshToken))
                               throw new ArgumentNullException("refreshToken");
                  
                           _store = new Dictionary<string, TokenResponse>();
                  
                           // add new entry
                           StoreAsync<TokenResponse>(key,
                               new TokenResponse() { RefreshToken = refreshToken, TokenType = "Bearer" }).Wait();
                       }
                  
                       /// <summary>
                       /// Remove all items
                       /// </summary>
                       /// <returns></returns>
                       public async Task ClearAsync()
                       {
                           await Task.Run(() =>
                           {
                               _store.Clear();
                               _stringStore.Clear();
                           });
                       }
                  
                       /// <summary>
                       /// Remove single entry
                       /// </summary>
                       /// <typeparam name="T"></typeparam>
                       /// <param name="key"></param>
                       /// <returns></returns>
                       public async Task DeleteAsync<T>(string key)
                       {
                           await Task.Run(() =>
                           {
                              // check type
                               AssertCorrectType<T>();
                  
                               if (typeof(T) == typeof(string))
                               {
                                   if (_stringStore.ContainsKey(key))
                                       _stringStore.Remove(key);                 
                               }
                               else if (_store.ContainsKey(key))
                               {
                                   _store.Remove(key);
                               }
                           });
                       }
                  
                       /// <summary>
                       /// Obtain object
                       /// </summary>
                       /// <typeparam name="T"></typeparam>
                       /// <param name="key"></param>
                       /// <returns></returns>
                       public async Task<T> GetAsync<T>(string key)
                       {
                           // check type
                           AssertCorrectType<T>();
                  
                           if (typeof(T) == typeof(string))
                           {
                               if (_stringStore.ContainsKey(key))
                                   return await Task.Run(() => { return (T)(object)_stringStore[key]; });
                           }
                           else if (_store.ContainsKey(key))
                           {
                               return await Task.Run(() => { return (T)(object)_store[key]; });
                           }
                           // key not found
                           return default(T);
                       }
                  
                       /// <summary>
                       /// Add/update value for key/value
                       /// </summary>
                       /// <typeparam name="T"></typeparam>
                       /// <param name="key"></param>
                       /// <param name="value"></param>
                       /// <returns></returns>
                       public Task StoreAsync<T>(string key, T value)
                       {
                           return Task.Run(() =>
                           {
                               if (typeof(T) == typeof(string))
                               {
                                   if (_stringStore.ContainsKey(key))
                                       _stringStore[key] = (string)(object)value;
                                   else
                                       _stringStore.Add(key, (string)(object)value);
                               } else
                               {
                                   if (_store.ContainsKey(key))
                                       _store[key] = (TokenResponse)(object)value;
                                   else
                                       _store.Add(key, (TokenResponse)(object)value);
                               }
                           });
                       }
                  
                       /// <summary>
                       /// Validate we can store this type
                       /// </summary>
                       /// <typeparam name="T"></typeparam>
                       private void AssertCorrectType<T>()
                       {
                           if (typeof(T) != typeof(TokenResponse) && typeof(T) != typeof(string)) 
                               throw new NotImplementedException(typeof(T).ToString());
                       }
                   }
                  

                  这篇关于MVC 中的 GoogleWebAuthorizationBroker 用于 Google Drive 访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Google Drive - 如何从代码中清空垃圾(以编程方式 下一篇:Google 云端硬盘 - 无法通过经过身份验证的 (OAut

                  相关文章

                  最新文章

                    <bdo id='eG58L'></bdo><ul id='eG58L'></ul>
                • <i id='eG58L'><tr id='eG58L'><dt id='eG58L'><q id='eG58L'><span id='eG58L'><b id='eG58L'><form id='eG58L'><ins id='eG58L'></ins><ul id='eG58L'></ul><sub id='eG58L'></sub></form><legend id='eG58L'></legend><bdo id='eG58L'><pre id='eG58L'><center id='eG58L'></center></pre></bdo></b><th id='eG58L'></th></span></q></dt></tr></i><div id='eG58L'><tfoot id='eG58L'></tfoot><dl id='eG58L'><fieldset id='eG58L'></fieldset></dl></div>
                  1. <tfoot id='eG58L'></tfoot>

                    <legend id='eG58L'><style id='eG58L'><dir id='eG58L'><q id='eG58L'></q></dir></style></legend>

                    <small id='eG58L'></small><noframes id='eG58L'>