Click here to Skip to main content
15,913,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

When I use X509Certificate2 for authentication Service account, i receive an access_token.
after I request "https://www.googleapis.com/tracks/v1/entities/create[^]"

appear error "The remote server returned an error: (401) Unauthorized."

My Code:
VB
Private Sub AuthV2()
        Dim client_id As String = "XXX@developer.gserviceaccount.com"
        Dim key As String = "XXX-privatekey.p12"
        Dim client As New WebClient()
        Dim formData As New NameValueCollection()
        Dim SCOPE As String = "https://www.googleapis.com/auth/tracks"
        Dim now As Long = unix_timestamp()
        Dim exp As Long = now + 3600
        Dim jwt_header As String = "{""alg"":""RS256"",""typ"":""JWT""}"
        Dim claim As String = "{""iss"":""" & client_id & """,""scope"":""" & SCOPE & """,""aud"":""https://accounts.google.com/o/oauth2/token"",""exp"":" & exp & ",""iat"":" & now & "}"
        Dim e1 As New System.Text.ASCIIEncoding()
        Dim clearjwt As String = Base64UrlEncode(e1.GetBytes(jwt_header)) & "." & Base64UrlEncode(e1.GetBytes(claim))
        Dim buffer As Byte() = Encoding.[Default].GetBytes(clearjwt)
        Dim cert As New X509Certificate2(key, "notasecret")
        Dim cp As New CspParameters(24, "Microsoft Enhanced RSA and AES Cryptographic Provider", DirectCast(cert.PrivateKey, RSACryptoServiceProvider).CspKeyContainerInfo.KeyContainerName)
        Dim provider As New RSACryptoServiceProvider(cp)
        Dim signature As Byte()
        signature = provider.SignData(buffer, "SHA256")
        Dim assertion As String = clearjwt & "." & Base64UrlEncode(signature)
        formData("grant_type") = "assertion"
        formData("assertion_type") = "http://oauth.net/grant_type/jwt/1.0/bearer"
        formData("assertion") = assertion
        Try
            client.Headers("Content-type") = "application/x-www-form-urlencoded"
            Dim responseBytes As Byte() = client.UploadValues("https://accounts.google.com/o/oauth2/token", "POST", formData)
            Dim Result As String = Encoding.UTF8.GetString(responseBytes)
            Dim tokens As String() = Result.Split(":"c)
            For i As Integer = 0 To tokens.Length - 1
                If tokens(i).Contains("access_token") Then
                    Me.lauth_token = (tokens(i + 1).Split(","c)(0).Replace("""", ""))
                End If
            Next
        Catch ex As WebException
        End Try
        RequestGoogleAPI()
    End Sub

Private Sub RequestGoogleAPI()
        Dim request As WebRequest
        Dim postData As String
        Dim byteArray As Byte() = Nothing
        Dim dataStream As Stream
        Dim response As WebResponse
        Try
            request = WebRequest.Create("https://www.googleapis.com/tracks/v1/entities/create")
            postData = "{""entities"":[{""name"":""ABC"",""type"":""AUTOMOBILE""},{""name"":""EFG"",""type"":""AUTOMOBILE""}]}"
            byteArray = Encoding.UTF8.GetBytes(postData)
            request.ContentType = " application/json"
            request.ContentLength = byteArray.Length
            request.Timeout = 10000
            request.Method = "POST"
            'request
            dataStream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()

            'response
            response = request.GetResponse() '' error "The remote server returned an error: (401) Unauthorized."
            dataStream = response.GetResponseStream()
        Catch ex As Exception
        End Try        
    End Sub


please help me, thank you very much!
Posted

1 solution

If you have proxy you need to pass the credentials to the request object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900