# Google APIs
使用帶有VBA-Web的Google API的示例包含在項目的`examples /`文件夾中。 一些細節:
## [](https://github.com/VBA-tools/VBA-Web/wiki/Google-APIs#authentication)Authentication授權
有許多方法可以使用Google的OAuth 2.0身份驗證,但是要與VBA-Web一起使用,不能使用任何需要回調網址的方法。 這限制了[已安裝的應用程序/設備](https://developers.google.com/accounts/docs/OAuth2#installed)的身份驗證方法。
1. 轉到[Google的API控制臺](https://code.google.com/apis/console#access)以使用Google創建新的應用程序。 在“客戶端ID設置”部分,選擇“已安裝的應用程序和其他”。 存儲生成的客戶端ID和密碼。
2. 確定應用程序的所需范圍。 例如,要訪問Google Analytics數據,需要在范圍內將其定義為`https://www.googleapis.com/auth/analytics.readonly`。 通常,每個范圍都以`https://www.googleapis.com/auth/`作為前綴,然后添加特定范圍。 有關可用范圍的列表,請訪問[Google OAuth 2.0 Playground](https://developers.google.com/oauthplayground/)
3. 將`GoogleAuthenticator`類添加到項目中并按如下方式進行設置:
~~~vbnet
Dim Auth As New GoogleAuthenticator
Auth.Setup _
ClientId:="ClientId", _
ClientSecret:="ClientSecret"
Auth.Scope = Array("https://www.googleapis.com/auth/analytics.readonly")
Auth.Login
Set Client.Authenticator = Auth
~~~
## [](https://github.com/VBA-tools/VBA-Web/wiki/Google-APIs#example-request)Example Request請求范例
通過客戶端的身份驗證設置,您可以使用正常的請求配置。 以下是用于檢索Google Analytics數據以及如何設置請求的所需網址示例:
~~~vbnet
'// 期望的請求: GET https://www.googleapis.com/analytics/v3/data/ga
'// ?ids=ga:12345
'// &start-date=2008-10-01
'// &end-date=2008-10-31
'// &metrics=ga:visits,ga:bounces
'// Client.BaseUrl = "https://www.googleapis.com/analytics/v3/"
Public Function AnalyticsRequest(ProfileId As String, StartDate As Date, EndDate As Date) As RestRequest
Set AnalyticsRequest = New WebRequest
AnalyticsRequest.Resource = "data/ga"
AnalyticsRequest.Method = WebMethod.HttpGet
AnalyticsRequest.AddQuerystringParam "ids", "ga:" & ProfileId
AnalyticsRequest.AddQuerystringParam "start-date", Format(StartDate, "yyyy-mm-dd")
AnalyticsRequest.AddQuerystringParam "end-date", Format(EndDate, "yyyy-mm-dd")
AnalyticsRequest.AddQuerystringParam "metrics", "ga:visits,ga:bounces"
End Function
~~~
### [](https://github.com/VBA-tools/VBA-Web/wiki/Google-APIs#links)鏈接:
* [使用OAuth 2.0訪問Google API,已安裝的應用程序/設備](https://developers.google.com/accounts/docs/OAuth2#installed)
* [將OAuth 2.0用于已安裝的應用程序](https://developers.google.com/accounts/docs/OAuth2InstalledApp)
* [Google OAuth 2.0 Playground](https://developers.google.com/oauthplayground/)
- README
- 指南
- 概述
- GET Request
- WebRequest
- 屬性
- Resource
- Method
- Body
- Format
- RequestFormat
- ResponseFormat
- CustomRequestFormat
- CustomResponseFormat
- ContentType
- Accept
- ContentLength
- FormattedResource
- Cookies
- Headers
- QuerystringParams
- UrlSegments
- 方法
- AddHeader
- SetHeader
- AddUrlSegment
- AddQuerystringParam
- AddCookie
- AddBodyParameter
- CreateFromOptions
- WebClient
- 屬性
- BaseUrl
- Authenticator
- TimeoutMs
- ProxyServer
- ProxyBypassList
- ProxyUsername
- ProxyPassword
- EnableAutoProxy
- Insecure
- FollowRedirects
- 方法
- Execute
- GetJson
- PostJson
- SetProxy
- GetFullUrl
- WebResponse
- 屬性
- StatusCode
- StatusDescription
- Content
- Data
- Body
- Headers
- Cookies
- 方法
- Update
- WebHelpers
- 屬性
- WebStatusCode
- WebMethod
- WebFormat
- UrlEncodingMode
- EnableLogging
- 方法
- LogDebug
- LogWarning
- LogError
- LogRequest
- LogResponse
- Obfuscate
- ParseJson
- ConvertToJson
- ParseUrlEncoded
- ConvertToUrlEncoded
- ParseXml
- ConvertToXml
- ParseByFormat
- ConvertToFormat
- UrlEncode
- UrlDecode
- Base64Encode
- Base64Decode
- RegisterConverter
- JoinUrl
- UrlParts
- CloneDictionary
- CloneCollection
- CreateKeyValue
- FindInKeyValues
- AddOrReplaceInKeyValues
- FormatToMediaType
- MethodToName
- HMACSHA1
- HMACSHA256
- MD5
- CreateNonce
- IWebAuthenticator
- 方法
- BeforeExecute
- AfterExecute
- PrepareHttp
- PrepareCurl
- WebAsyncWrapper
- 屬性
- Client
- 方法
- ExecuteAsync
- 范例
- Salesforce網站
- Google APIs
- Todoist API
- 其他主題
- 調試
- 授權
- 實現自己的IWebAuthenticator
- Url編碼