2014-11-23 91 views

回答

5

您可以繼承表單FlowMetadata並覆蓋AuthCallback屬性。看看下面的鏈接:

https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.Auth.Mvc4/OAuth2/Mvc/FlowMetadata.cs?r=eb702f917c0e18fc960d077af132d0d83bcd6a88#49

但是,你將能夠改變相對URL但不是絕對的。

如果你想使用一個完全不同的網址,你需要做的創建自己的AuthorizationCodeMvcApp並改變其構造函數如下:

public MyNewAuthorizationCodeMvcApp(Controller controller, FlowMetadata flowData) 
     : base(
     flowData.Flow, 
     < YOUR URL HERE >, 
     controller.Request.Url.ToString()) 
    { 
     this.controller = controller; 
     this.flowData = flowData; 
    } 

然後,你可以把它插入到你的流量,而不是默認AuthorizationCodeMvcApp(庫的默認實現)。

+0

感謝您的回答。我嘗試,但有新的錯誤))我嘗試寫在這裏,但主持人說,我需要打開新的問題。鏈接到我的新錯誤^ http://stackoverflow.com/questions/27104306/change-redirect-url-in-google-drive-c-sharp-part-2 – 2014-11-24 11:57:29

5

我也發現它變得非常棘手,首先要改變谷歌OAuth 2.0重定向uri,但事實證明它很簡單。你可以用不同的方式做到這一點。如果您按照Googles指南獲取OAuth 2.0 Web應用程序(ASP.NET MVC),最簡單的選擇是覆蓋AppFlowMetadata類中的AuthCallback字符串。

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

public override string AuthCallback 
{ 
    get { return @"/AuthCallback/Index"; } 
} 

你也可以實現自己的 「AuthorizationCodeMvcApp」 的版本,但是那是相當矯枉過正。不要越過河流得到水。 :)

https://github.com/google/google-api-dotnet-client/tree/master/Src/GoogleApis.Auth.Mvc4/OAuth2/Mvc

但是,如果你想要做的是,這裏有一個例子:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Web.Mvc; 
using Google.Apis.Auth.OAuth2.Mvc; 
using Google.Apis.Auth.OAuth2.Web; 

namespace ProjectName.GoogleCalendar 
{ 
     /// <summary> 
     /// Thread-safe OAuth 2.0 authorization code flow for a MVC web application that persists end-user credentials. 
     /// </summary> 
     public class CustomAuthorizationCodeMvcApp : AuthorizationCodeWebApp 
     { 

      private readonly Controller controller; 
      private readonly FlowMetadata flowData; 

      /// <summary>Gets the controller which is the owner of this authorization code MVC app instance.</summary> 
      public Controller Controller { get { return controller; } } 

      /// <summary>Gets the <see cref="Google.Apis.Auth.OAuth2.Mvc.FlowMetadata"/> object.</summary> 
      public FlowMetadata FlowData { get { return flowData; } } 

      /// <summary>Constructs a new authorization code MVC app using the given controller and flow data.</summary> 
      public CustomAuthorizationCodeMvcApp(Controller controller, FlowMetadata flowData) 
       : base(
       flowData.Flow, 
       new Uri(controller.Request.Url.GetLeftPart(UriPartial.Authority) + "/CustomController" + flowData.AuthCallback).ToString(), 
       controller.Request.Url.ToString()) 
      { 
       this.controller = controller; 
       this.flowData = flowData; 
      } 

      /// <summary> 
      /// Asynchronously authorizes the installed application to access user's protected data. It gets the user 
      /// identifier by calling to <see cref="Google.Apis.Auth.OAuth2.Mvc.FlowMetadata.GetUserId"/> and then calls to 
      /// <see cref="Google.Apis.Auth.OAuth2.AuthorizationCodeWebApp.AuthorizeAsync"/>. 
      /// </summary> 
      /// <param name="taskCancellationToken">Cancellation token to cancel an operation</param> 
      /// <returns> 
      /// Auth result object which contains the user's credential or redirect URI for the authorization server 
      /// </returns> 
      public Task<AuthResult> AuthorizeAsync(CancellationToken taskCancellationToken) 
      { 
       return base.AuthorizeAsync(FlowData.GetUserId(Controller), taskCancellationToken); 
      } 
     } 
    } 
+0

這是最簡單的方法,非常有幫助。 – Dhanuka777 2016-08-03 06:08:26