如何在vb.net服务应用中使用Google.Apis.Analytics.v3 API

时间:2015-06-01 09:56:40

标签: vb.net google-analytics-api

我已经设法将我的自我成功地用于google等。但现在我需要帮助如何使用Google.Apis.Analytics.v3或Google.Apis.Analytics.v3.Data dll来检索数据。例如,我需要上个月的游客。到目前为止,我有这个:

Imports System.Security.Cryptography.X509Certificates
Imports System.Threading
Imports Google.Apis.Auth.OAuth2
Imports MailBee
Imports MailBee.ImapMail
Imports MailBee.SmtpMail
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Imports Google.Apis.Analytics.v3
Imports Google.Apis.Analytics.v3.Data
Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
Imports Google.Apis.Services
Imports Google.Apis.Util


Public Class gaimport

    Inherits System.Web.UI.Page

    Public Shared Sub Main(sender As Object, e As EventArgs)
        Dim serviceAccountEmail As String = "xxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com"
        Dim userEmail As String = "xxxxx@gmail.com"

        Dim certificate As New X509Certificate2("c:\works\gaimport\gaimport\Analyticsdata-xxxxxxxxxxxx.p12", _
            "notasecret", X509KeyStorageFlags.Exportable)

        Dim credential As New ServiceAccountCredential( _
            New ServiceAccountCredential.Initializer(serviceAccountEmail) With { _
            .Scopes = New String() {"https://www.googleapis.com/auth/analytics.readonly"} _
        }.FromCertificate(certificate))

        If credential.RequestAccessTokenAsync(CancellationToken.None).Result Then
            Dim xoauthKey As String = OAuth2.GetXOAuthKeyStatic(userEmail, credential.Token.AccessToken)



            Console.WriteLine("test" & xoauthKey)


        End If

    End Sub







End Class

现在我需要一些东西来让分析部分工作。我该如何使用那些dll?我可以从谷歌订购来源(就像我使用clientId登录一样)吗?

1 个答案:

答案 0 :(得分:0)

我找到了一个很好的解决方案来解决我的问题(在获得访问令牌之后):

{$DebugInfo ON}
{$LocalSymbols ON}

然后,我只是想让JSON得到我真正想要的东西:

Dim webClient As New System.Net.WebClient
webClient.Encoding = System.Text.Encoding.UTF8
Dim requesturl = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A" & domainidid(domainrno) & "&start-date=" & viimekuunalkuset & "&end-date=" & viimekuunloppuset & "&metrics=ga%3Ausers%2Cga%3Asessions%2Cga%3Apageviews&access_token=" & credential.Token.AccessToken

Dim resultb As String = webClient.DownloadString(requesturl)