Dav*_*aev 3 c# google-api asp.net-mvc-4 google-api-dotnet-client
我试试这段代码:https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web_applications
我的代码一样
但!我需要更改此默认网址重定向.现在是:redirect_uri = http:%2F%2Flocalhost:52674%2FAuthCallback%2FIndexAsync
我怎么能改变这个网址?伙计们请帮忙.
谢谢
您可以继承表单FlowMetadata并覆盖AuthCallback属性.看看以下链接:
但是,您将能够更改相对URL但不能更改绝对URL.
如果要使用完全不同的URL,则需要创建自己的AuthorizationCodeMvcApp并将其构造函数更改为以下内容:
public MyNewAuthorizationCodeMvcApp(Controller controller, FlowMetadata flowData)
: base(
flowData.Flow,
< YOUR URL HERE >,
controller.Request.Url.ToString())
{
this.controller = controller;
this.flowData = flowData;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将其插入到您的流中,而不是默认的AuthorizationCodeMvcApp(库的默认实现).
我还发现最初改变Googles OAuth 2.0重定向uri真的很棘手但事实证明它非常简单.你可以用不同的方式做到这一点.如果您关注OAog 2.0 Web应用程序(ASP.NET MVC)的Googles指南,最简单的选择是覆盖AppFlowMetadata类中的AuthCallback字符串.
https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth
public override string AuthCallback
{
get { return @"/AuthCallback/Index"; }
}
Run Code Online (Sandbox Code Playgroud)
您也可以实现自己的"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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2403 次 |
| 最近记录: |