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
});
}
}