如何撤消用户的令牌

时间:2015-06-01 12:25:23

标签: vb.net google-api google-oauth gmail-api

如何撤消用户的令牌?

这是我的验证码,我想重置(撤销)一个用户的访问权限。这是因为我使用了GMail API,我希望允许我的用户在必要时更改他们的电子邮件。

实际上,令牌会在达到令牌的时刻保存,但我必须在用户决定更改其电子邮件时更改此值。

Dim datafolder As String = Server.MapPath("App_Data/GoogleService.api.auth.store")
        Dim scopes As IList(Of String) = New List(Of String)()
        Dim UserId As String = Context.User.Identity.Name.ToString

        scopes.Add(DriveService.Scope.Drive)
        scopes.Add(GmailService.Scope.MailGoogleCom)
        Dim myclientsecret As New ClientSecrets() With { _
          .ClientId = CLIENT_ID, _
          .ClientSecret = CLIENT_SECRET _
        }

        Dim flow As GoogleAuthorizationCodeFlow

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

        Dim uri As String = Request.Url.ToString()

        Dim code = 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, Request("state")).Result
            Response.Redirect(oauthState)
        Else
            Dim result = New AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result
            If result.RedirectUri IsNot Nothing Then
                ' Redirect the user to the authorization server.
                Response.Redirect(result.RedirectUri)
            Else
                ' The data store contains the user credential, so the user has been already authenticated.
                myDriveService = New DriveService(New BaseClientService.Initializer() With { _
                  .ApplicationName = "Liens Google SyGED", _
                  .HttpClientInitializer = result.Credential _
                })

                myGMailService = New GmailService(New BaseClientService.Initializer() With { _
               .ApplicationName = "Liens Google SyGED", _
               .HttpClientInitializer = result.Credential _
             })

            End If
        End If

1 个答案:

答案 0 :(得分:0)