2

我一直在整个互联网上进行搜索,此外还就这个主题与谷歌的服务台人员保持联系。到目前为止没有运气。

我正在尝试通过 VBA 中的 Web 服务调用使用 Google API for Directions。我的代码工作得非常好,它从 web 服务中给出了正确的结果。但是,如果我想发送超过几千个每日请求,他们要求我在将数据发送给他们之前对其进行加密。

他们在下面有一个代码,但与 VBA 无关,仅适用于 VB.net。

这里有人熟悉 VBA 中的代码可用性吗?VBA 是否具有所需的库,可以对代码进行一些小的调整,或者?

Option Explicit On
Option Strict On

Imports System
Imports System.Security.Cryptography
Imports System.Text

Namespace SignUrl
    Public Module GoogleUrlSigner
        Public Function Sign(ByVal url As String, ByVal keyString As String) As String
            Dim encoding As ASCIIEncoding = New ASCIIEncoding()
        'URL-safe decoding
        Dim privateKeyBytes As Byte() = Convert.FromBase64String(keyString.Replace("-", "+").Replace("_", "/"))

        Dim objURI As Uri = New Uri(url)
        Dim encodedPathAndQueryBytes As Byte() = encoding.GetBytes(objURI.LocalPath & objURI.Query)

        'compute the hash
        Dim algorithm As HMACSHA1 = New HMACSHA1(privateKeyBytes)
        Dim hash As Byte() = algorithm.ComputeHash(encodedPathAndQueryBytes)

        'convert the bytes to string and make url-safe by replacing '+' and '/' characters
        Dim signature As String = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_")

        Add the signature to the existing URI.
        Return objURI.Scheme & "://" & objURI.Host & objURI.LocalPath & objURI.Query & "&signature=" & signature
    End Function
End Module

Public Module Program
    Sub Main()
        Const keyString As String = "vNIXE0xscrmjlyV-12Nj_BvUPaw="

        'The URL shown here is a static URL which should be already
        'URL-encoded. In practice, you will likely have code
        'which assembles your URL from user or web service input
        'and plugs those values into its parameters.
        Dim urlString As String = "http://maps.google.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"

        Console.WriteLine(GoogleUrlSigner.Sign(urlString, keyString))
    End Sub
End Module
End Namespace
4

0 回答 0