本文介绍了使用访问令牌来请求Google地图跟踪API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
当我使用X509Certificate2进行身份验证服务帐户时,我会收到一个access_token。
我请求 []
出现错误远程服务器返回错误:(401)未经授权。
我的代码:
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:
Private Sub AuthV2()
Dim client_id As String = "[email protected]"
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!
推荐答案
这篇关于使用访问令牌来请求Google地图跟踪API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!