代码试图找到ServiceAccountCredential.cs文件,同时使用OAUTH 2.0访问谷歌分析

本文关键字:OAUTH 访问 谷歌 ServiceAccountCredential 文件 cs 代码 | 更新日期: 2023-09-27 18:01:47

我试图使用OAUTH 2.0认证访问ga:visitsga:transactions的谷歌分析数据。我遵循了以下步骤:

  1. 在https://code.google.com/apis/console/网站上启用了Google Analytics API。另外,创建了一个客户端ID并保存了。p12密钥
  2. 生成的电子邮件id在Analytics网站上提供Read和Analyze访问权限。
  3. 我创建了一个Windows窗体应用程序,并使用package命令下载了NuGet包:—Install-Package google . api . analytics .v3所有引用包括Google.Apis,Google.Apis. analytics .v3,Google.Apis. auth,Google.Apis. auth . platformservices,Google.Apis. core,Google.Apis。平台服务被添加到项目

此外,在访问数据时遇到错误:-

查找来源c:'code'google.com'google-api-dotnet-client'default'Tools'Google.Apis.Release'bin'Debug'test'default'Src'GoogleApis.Auth.DotNet4'OAuth2'ServiceAccountCredential.cs

,同时运行以下代码行:

var credential = new ServiceAccountCredential(new     ServiceAccountCredential.Initializer(serviceAccountEmail)         
 {    
    Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }    
 }.FromCertificate(cert));   

你能帮我解决这个错误吗?请查看下面使用的代码:-

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using Google.Apis.Analytics.v3;  
using System.Security.Cryptography.X509Certificates;  
using Google.Apis.Auth.OAuth2;  
using Google.Apis.Services;  
namespace WindowsFormsApplication5  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
        private void Form1_Load(object sender, EventArgs e)
        {
            string keyFilePath = "C:''Fetch GA Data-348e7435108b.p12";
            string serviceAccountEmail= "xx@developer.gserviceaccount.com";     
            string keyPassword = "notasecret";
            string websiteCode = "8482xxxx";     
            AnalyticsService service = null; 
            //loading the Key file     
            var certificate = new X509Certificate2(keyFilePath, keyPassword, X509KeyStorageFlags.Exportable);  
            //Add Scopes
            var scopes =     
            new string[] { AnalyticsService.Scope.Analytics,AnalyticsService.Scope.AnalyticsEdit,AnalyticsService.Scope.AnalyticsManageUsers,AnalyticsService.Scope.AnalyticsReadonly   
};       
            //create a new ServiceAccountCredential 
            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)     
            {
                //Scopes = scopes     
                Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }
            }.FromCertificate(cert));    
            //Create a Service
            service = new AnalyticsService(new BaseClientService.Initializer()    
            {    
                HttpClientInitializer = credential    
            }); 

            DataResource.GaResource.GetRequest request = service.Data.Ga.Get(     
              "ga:" + websiteCode, "2015-05-27", "2015-05-27","ga:visits");     
            request.Dimensions = "ga:year,ga:month,ga:day";     
            var data = request.Execute();   
            }

        }
}

代码试图找到ServiceAccountCredential.cs文件,同时使用OAUTH 2.0访问谷歌分析

我不知道你从哪里得到的例子,但它甚至不接近你应该如何使用一个服务帐户访问谷歌分析api。

 string[] scopes = 
          new string[] { 
             AnalyticsService.Scope.Analytics,                 // view and manage your Google Analytics data
             AnalyticsService.Scope.AnalyticsManageUsers};     // View Google Analytics data      
string keyFilePath = @"c:'file.p12" ;    // found in developer console
string serviceAccountEmail = "xx@developer.gserviceaccount.com";  // found in developer console
//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail)
                                                                  {
                                                                   Scopes = scopes
                                                                 }.FromCertificate(certificate));

AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
            {
             HttpClientInitializer = credential,
             ApplicationName = "Analytics API Sample",
            });

代码摘自Google Analytics Authentication c#教程系列

小费服务帐户设置

从谷歌开发者控制台获取服务帐户的电子邮件地址,我们需要给这个电子邮件地址访问我们的谷歌分析数据。我们这样做是通过添加它,因为我们会在谷歌分析网站的管理部分的任何其他用户。作为帐户级别的用户添加服务帐户电子邮件地址必须是帐户级别。这将使您的服务帐户能够访问有关的Google Analytics帐户。我有没有提到它必须在ACCOUNT级别?