Change default redirect for google - c # (google liblary)

I am trying this code: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web_applications

my code is the same

But! I need to change this default url redirect. Now this is: redirect_uri = http:% 2F% 2Flocalhost: 52674% 2FAuthCallback% 2FIndexAsync

How can I change this URL? Guys, please help.

thank

+3


source to share


2 answers


You can inherit from the FlowMetadata form and override the AuthCallback property. Take a look at the following link:

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

BUT, you will be able to change the relative url, but not the absolute one.



If you want to use a completely different URL, you will need to create your own AuthorizationCodeMvcApp and change its constructor to the following:

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

      

Then you can hook it up to a thread instead of the default AuthorizationCodeMvcApp (standard library implementation).

+5


source


I also found that it was very difficult to change Googles OAuth 2.0 redirect uri at first, but it turned out to be quite simple. You can do this in different ways. If you are following the Googles Guide for OAuth 2.0 Web Applications (ASP.NET MVC), the easiest choice is to override the AuthCallback string in your AppFlowMetadata class.

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

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

      

You can also implement your own version of "AuthorizationCodeMvcApp", but this is rather complicated. Don't cross the stream to get water. :)



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

But if you want to do this, here's an example:

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 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 credential or redirect URI for the authorization server
            /// </returns>
            public Task<AuthResult> AuthorizeAsync(CancellationToken taskCancellationToken)
            {
                return base.AuthorizeAsync(FlowData.GetUserId(Controller), taskCancellationToken);
            }
        }
    }

      

+5


source







All Articles