2015-10-11 102 views
0

如何在.NET中設置Google認證中的Approval_Prompt = force?Google Oauth批准提示.net

這裏是我的代碼:

Dim datafolder As String = Current.Server.MapPath("App_Data/CalendarService.api.auth.store") 
    Dim scopes As IList(Of String) = New List(Of String)() 
    Dim UserId As String = "GoogleID_" & pUsername   
    scopes.Add(CalendarService.Scope.Calendar) 

    Dim myclientsecret As New ClientSecrets() With { _ 
     .ClientId = _ClientID, _ 
     .ClientSecret = _ClientSecret _ 
    } 

    Dim flow As GoogleAuthorizationCodeFlow 

    flow = New GoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer() With { _ 
     .DataStore = New FileDataStore(datafolder), _ 
     .ClientSecrets = myclientsecret, _ 
     .Scopes = scopes _ 
    }) 


    Dim uri As String = Current.Request.Url.ToString() 'mengambil URL dari halaman ini 

    Dim code = Current.Request("code") 
    If code IsNot Nothing Then    
     Dim token = flow.ExchangeCodeForTokenAsync(UserId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result 

     ' Extract the right state. 
     Dim oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, Current.Request("state")).Result    
     Current.Response.Redirect(oauthState) 
    Else 
     result = New AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result 

     If result.RedirectUri IsNot Nothing Then 
      ' Redirect the user to the authorization server. 
      Current.Response.Redirect(result.RedirectUri) 
     Else 
      ' The data store contains the user credential, so the user has been already authenticated. 
      'result.Credential.RevokeTokenAsync(CancellationToken.None)     
      myCalendarservice = New CalendarService(New BaseClientService.Initializer() With { _ 
       .ApplicationName = "My Calendar", _ 
       .HttpClientInitializer = result.Credential _ 
      })     
      'result.Credential.RevokeTokenAsync(CancellationToken.None) 
     End If 
    End If 

所有我想要的只是爲了Approval_Prompt套入隊,但想不通...請有人幫助我......

+0

你檢查此鏈接https://developers.google.com/identity/protocols/OAuth2InstalledApp。它舉例說明如何在發送請求時將Approval_Prompt設置爲強制。 – SGC

+0

我無法使用OAuth2安裝的應用程序。 – Oscar

回答

0

我已經解決了我的問題。我手動添加「approval_prompt =力」在我的代碼

昏暗流作爲GoogleAuthorizationCodeFlow

flow = New GoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer() With { _ 
    .DataStore = New FileDataStore(datafolder), _ 
    .ClientSecrets = myclientsecret, _ 
    .Scopes = scopes _ 
}) 


Dim uri As String = Current.Request.Url.ToString() 'mengambil URL dari halaman ini 

Dim code = Current.Request("code") 
If code IsNot Nothing Then    
    Dim token = flow.ExchangeCodeForTokenAsync(UserId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result 

    ' Extract the right state. 
    Dim oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, Current.Request("state")).Result    
    Current.Response.Redirect(oauthState) 
Else 
    result = New AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result 

    If result.RedirectUri IsNot Nothing Then 
     ' Redirect the user to the authorization server. 
     Current.Response.Redirect(result.RedirectUri & "approval_prompt=force") 
    Else 
     ' The data store contains the user credential, so the user has been already authenticated. 
     'result.Credential.RevokeTokenAsync(CancellationToken.None)     
     myCalendarservice = New CalendarService(New BaseClientService.Initializer() With { _ 
      .ApplicationName = "My Calendar", _ 
      .HttpClientInitializer = result.Credential _ 
     })     
     'result.Credential.RevokeTokenAsync(CancellationToken.None) 
    End If 
End If