Google Analytics OAuth with AccessType = Offline in C#

夙愿已清 提交于 2019-11-28 08:50:18

I know only one way: you need to override GoogleAuthorizationCodeRequestUrl, but I have no idea how to use this with AuthorizationBroker.

internal class ForceOfflineGoogleAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
    public ForceOfflineGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer) : base(initializer) { }

    public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
    {
        return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
                {
                    ClientId = ClientSecrets.ClientId,
                    Scope = string.Join(" ", Scopes),
                    RedirectUri = redirectUri,
                    AccessType = "offline",
                    ApprovalPrompt = "force"
                };
    }
};

Looks like they create Flow inside the broker: GoogleWebAuthorizationBroker.cs

And I didn't see any way to pass params or replace AuthorizationCodeFlow

Apparently I can't comment, but to extend on Darida's answer:

Make your custom CodeFlow

public class CustomAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
    public CustomAuthorizationCodeFlow(GoogleAuthorizationCodeFlow.Initializer initializer) : base(initializer) { }

    public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(String redirectUri)
    {
        return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
        {
            ClientId = ClientSecrets.ClientId,
            Scope = string.Join(" ", Scopes),
            RedirectUri = redirectUri,
            AccessType = "online",
            ApprovalPrompt = "auto"
        };
    }
}

Then make a custom FlowMetadata

public class AppFlowMetadata : FlowMetadata
{
    private static readonly IAuthorizationCodeFlow flow =
        new CustomAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = "...",
                ClientSecret = "..."
            },
            Scopes = new String[] { AnalyticsService.Scope.AnalyticsReadonly },
            DataStore = new EFDataStore(),
        });

    public override IAuthorizationCodeFlow Flow
    {
        get { return flow; }
    }

    public override String GetUserId(Controller controller)
    {
        // In this sample we use the session to store the user identifiers.
        // That's not the best practice, because you should have a logic to identify
        // a user. You might want to use "OpenID Connect".
        // You can read more about the protocol in the following link:
        // https://developers.google.com/accounts/docs/OAuth2Login.

        return String.Format("user-{0}", WebSecurity.GetUserId(controller.User.Identity.Name));
    }
}

and then in the Controller

public ActionResult Sample()
{
    var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);

    if (result.Credential != null)
    {
        var service = new AnalyticsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = result.Credential,
            ApplicationName = APPLICATION_NAME
        });
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!