【问题标题】:Code trying to find ServiceAccountCredential.cs file while accessing Google Analytics using OAUTH 2.0在使用 OAUTH 2.0 访问 Google Analytics 时尝试查找 ServiceAccountCredential.cs 文件的代码
【发布时间】:2015-09-26 05:02:19
【问题描述】:

我正在尝试使用 OAUTH 2.0 身份验证访问 ga:visitsga:transactions 的 Google Analytics(分析)数据。我已按照以下步骤操作:

  1. https://code.google.com/apis/console/ 网站上启用了 Google Analytics API。另外,创建了一个客户端 ID 并保存了 .p12 密钥
  2. 生成的电子邮件 ID 在 Analytics 网站上提供了读取和分析访问权限。
  3. 我创建了一个 Windows 窗体应用程序,还使用 ​​package 命令下载了 NuGet 包:- Install-Package Google.Apis.Analytics.v3 包括 Google.Apis,Google.Apis.Analytics.v3,Google.Apis.Auth,Google.Apis.Auth.PlatformServices,Google.Apis.Core,Google.Apis.PlatformServices 在内的所有引用都添加到项目中

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

查找来源 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();   

            }


        }
}

【问题讨论】:

    标签: c# google-oauth google-analytics-api google-api-dotnet-client


    【解决方案1】:

    我不确定您从哪里获得该示例,但它甚至与您应该如何使用服务帐户来访问 Google Analytics(分析)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#系列教程中翻录的代码

    TIP 服务帐户设置

    从 Google 开发者控制台获取服务帐户电子邮件地址,我们需要让此电子邮件地址访问我们的 Google Analytics(分析)数据。我们通过添加它来做到这一点,就像我们在 Google Analytics(分析)网站的管理部分中添加任何其他用户一样。将服务帐户电子邮件地址添加为帐户级别的用户,它必须是帐户级别。这将使您的服务帐户能够访问相关的 Google Analytics 帐户。我有没有提到它必须在帐户级别?

    【讨论】:

    • 嗨。感谢您的回复。我还编写了与您的建议类似的代码。请您看看下面的内容。
    • @DalmTo :- 我改进了我的问题并添加了与您提到的代码类似的代码。但是,它在尝试创建 ServiceAccountCredential 时失败并出现错误。请您检查并分享您的意见。提前致谢。
    • 检查 FromCertificate(cert));什么证书?
    • 你看我的教程了吗,github上有一个示例项目你可以下载。
    • 我确实浏览了示例项目,但仍然遇到相同的错误。我检查了 cert 的值,它显示 cert 是:- {[Subject] CN=423234456098-7dnefrvrr80ie6kt1hse271if8bq9lnr.apps.googleusercontent。 com[Issuer] CN=423234456098-7dnefrvrr80ie6kt1hse271if8bq9lnr.apps.googleusercontent.com[序列号] 2D8A496E069713A9[Not Before] 6/24/2015 2:52:29 AM[Not After] 6/21/2025 2:52:29 AM [指纹] 348E7435108B8A642DE356D3F63606435666ABFE}
    猜你喜欢
    • 2017-08-06
    • 2015-10-02
    • 1970-01-01
    • 2015-11-11
    • 2018-08-20
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2011-11-25
    相关资源
    最近更新 更多