3

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

my code same

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

How I can change this url? Guys please help.

Thank you

David Abaev
  • 43
  • 1
  • 5

2 Answers2

8

I also found it really tricky at first to change Googles OAuth 2.0 redirect uri but it turns out it is quite simple. You can do this in different ways. If you follow Googles guide for OAuth 2.0 Web applications (ASP.NET MVC) the easiest choice is to override 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 could also implement your own version of "AuthorizationCodeMvcApp" but that is quite overkill. 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 that here is 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'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);
            }
        }
    }
Ogglas
  • 62,132
  • 37
  • 328
  • 418
5

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

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

BUT, You will be able to change the relative URL but not the absolute one.

If you want to use a totally different URL, you will need to do 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 plug it to your flow, instead of the default AuthorizationCodeMvcApp (the default implementation of the library).

peleyal
  • 3,472
  • 1
  • 14
  • 25
  • Thank you for answer. I try but have new error)) I try write here, but but moderator say that i need open new question. Link to my new errors^ http://stackoverflow.com/questions/27104306/change-redirect-url-in-google-drive-c-sharp-part-2 – David Abaev Nov 24 '14 at 11:57