0

issue: var result has access_token & token_id. how do i use this to get user email

here I am using google oauth to transfer user to google website loginto their account by using email&password

 Dim urlOAuth As String = "https://accounts.google.com/o/oauth2/auth"
 Dim urlGetToken As String = "https://accounts.google.com/o/oauth2/token"

' token url to get user information. need help to get user name or email here
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If String.IsNullOrEmpty(Request.QueryString("id")) = False Then
        Dim geturlCode As String = Request.QueryString("code")

        Dim WebRequest1 As HttpWebRequest = CType(WebRequest.Create(urlGetToken), HttpWebRequest)
        Dim Parameter As String = "code=" & geturlCode & "&client_id=" + clientID & "&client_secret=" + clientSecret & "&redirect_uri=" + redirecturi & "&grant_type=authorization_code"

        'Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Parameter)
        WebRequest1.Method = "POST"
        WebRequest1.ContentType = "application/x-www-form-urlencoded"
        ' WebRequest1.ContentLength = byteArray.Length

        Dim utfenc As UTF8Encoding = New UTF8Encoding()
        Dim bytes As Byte() = utfenc.GetBytes(Parameter)
        Dim os As Stream = Nothing
        Try
            WebRequest1.ContentLength = bytes.Length
            os = WebRequest1.GetRequestStream()
            os.Write(bytes, 0, bytes.Length)
        Catch
        End Try


        Dim WebResponse As HttpWebResponse = CType(WebRequest1.GetResponse(), HttpWebResponse)
        Dim responseStream As Stream = WebResponse.GetResponseStream()
        Dim responseStreamReader As StreamReader = New StreamReader(responseStream)
        ' access_token inside result
        Dim result = responseStreamReader.ReadToEnd()
    End If
End Sub

  'use oauth url so user can login in to there google account
    Public Sub LoginGoogleB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginGoogleB.Click
        Dim Uri As String = urlOAuth &
         "?approval_prompt=force" &
         "&scope=openid%20email" &
         "&response_type=code" &
         "&access_type=offline" &
         "&client_id=" & clientID &
         "&redirect_uri=" & redirecturi

    Response.Redirect(Uri)
    End Sub
Dan
  • 175
  • 1
  • 12
  • I would assume that the user had already provided all that info on a registration page and it would stored in a database. – Mary Oct 28 '21 at 02:18
  • When the user logs in and they send back data via the url like you said 'code' this is very important. You would then use this 'code' to call a google api that will return data that you want. Not sure which API endpoint that would be as not done it in a long time but normally something like 'UserInfo'. – AliK Oct 28 '21 at 06:10
  • @Mary, there is no database. user is login by google oauth – Dan Oct 28 '21 at 12:44
  • @Alik, o i see, so i will research how to use url 'code' to get user information – Dan Oct 28 '21 at 12:45
  • it will be really helpful if any one can provide me with a link as to how to use this 'code' variable to get user information in vb.net – Dan Oct 28 '21 at 12:54
  • [See if this helps](https://stackoverflow.com/questions/7130648/get-user-info-via-google-api) – AliK Oct 28 '21 at 14:38
  • thanks, i think i understand basic of oauth. we have to use oauth url for user to login to there account. than we have to use token url to some how to user information – Dan Oct 28 '21 at 16:05

0 Answers0