Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Analytics嵌入Api服务器端授权C#_C#_Google Analytics_Access Token - Fatal编程技术网

Google Analytics嵌入Api服务器端授权C#

Google Analytics嵌入Api服务器端授权C#,c#,google-analytics,access-token,C#,Google Analytics,Access Token,我正在尝试使用C#使用Google Analytics嵌入API服务器端授权,代码如下 public ActionResult Dashboard() { ViewBag.Message = "Dashboard."; var scopes = new string[] { AnalyticsService.Scope.Analytics, // view and manage your Google Analy

我正在尝试使用C#使用Google Analytics嵌入API服务器端授权,代码如下

  public ActionResult Dashboard()
    {
        ViewBag.Message = "Dashboard.";
        var scopes = new string[]
        {
            AnalyticsService.Scope.Analytics, // view and manage your Google Analytics data
            AnalyticsService.Scope.AnalyticsReadonly,
            AnalyticsService.Scope.AnalyticsEdit
        };
        const string serviceAccountEmail = "526261635050-fofbfeicvbpgumafa114te878787878@developer.gserviceaccount.com";  
        const string keyFilePath = @"D:\key.p12";

        var status = RequestAccessTokenAsync(keyFilePath,scopes,serviceAccountEmail);
        ViewBag.Token = _accessToken;

        return View();
    }

    private async Task<bool> RequestAccessTokenAsync(string certificateFile, string[] scope, string serviceAccount)
    {
        var certificate = new X509Certificate2(certificateFile, "notasecret", X509KeyStorageFlags.Exportable);
        var serviceAccountCredential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccount)
        {
            Scopes = scope
        }.FromCertificate(certificate));

        var status = await serviceAccountCredential.RequestAccessTokenAsync(CancellationToken.None);
        if (status)
            _accessToken = serviceAccountCredential.Token.AccessToken;
        return status;
    }
public ActionResult仪表板()
{
ViewBag.Message=“仪表板。”;
变量范围=新字符串[]
{
AnalyticsService.Scope.Analytics,//查看和管理您的谷歌分析数据
AnalyticsService.Scope.Analytics仅限,
AnalyticsService.Scope.AnalyticsEdit
};
const string serviceAccountEmail=“526261635050-fofbfeicvbpgumafa114te878787878@developer.gserviceaccount.com";  
常量字符串keyFilePath=@“D:\key.p12”;
var status=RequestAccessTokenAsync(keyFilePath、作用域、serviceAccountEmail);
ViewBag.Token=\u accessToken;
返回视图();
}
专用异步任务RequestAccessTokenAsync(字符串certificateFile、字符串[]范围、字符串serviceAccount)
{
var证书=新的X509Certificate2(certificateFile,“notasecret”,X509keystrageFlags.Exportable);
var serviceAccountCredential=新serviceAccountCredential(新serviceAccountCredential.Initializer(serviceAccount)
{
范围=范围
}.FromCertificate(证书));
var status=等待serviceAccountCredential.RequestAccessTokenAsync(CancellationToken.None);
如果(状态)
_accessToken=serviceAccountCredential.Token.accessToken;
返回状态;
}
制作一个服务实例可以很好地工作,并且还能够获取原始数据,但我们需要使用嵌入API,问题是在_accessToken中没有检索到值,我们需要能够访问嵌入API的值

任何想法/想法都会有帮助

在google演示站点上,提供的示例是针对python的-

尝试以下方法:

public ActionResult Dashboard()
{
    ViewBag.Message = "Dashboard.";
    var scopes = new string[]
    {
        AnalyticsService.Scope.Analytics, // view and manage your Google Analytics data
        AnalyticsService.Scope.AnalyticsReadonly,
        AnalyticsService.Scope.AnalyticsEdit
    };
    const string serviceAccountEmail = "526261635050-fofbfeicvbpgumafa114te878787878@developer.gserviceaccount.com";  
    const string keyFilePath = @"D:\key.p12";

    var certificate = new X509Certificate2(certificateFile, "notasecret", X509KeyStorageFlags.Exportable);
    var serviceAccountCredential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes = scope
    }.FromCertificate(certificate));

    Task<string> task = ((ITokenAccess)serviceAccountCredential).GetAccessTokenForRequestAsync();
    task.Wait();
    var _accessToken = task.Result;

    ViewBag.Token = _accessToken;

    return View();
}
public ActionResult仪表板()
{
ViewBag.Message=“仪表板。”;
变量范围=新字符串[]
{
AnalyticsService.Scope.Analytics,//查看和管理您的谷歌分析数据
AnalyticsService.Scope.Analytics仅限,
AnalyticsService.Scope.AnalyticsEdit
};
const string serviceAccountEmail=“526261635050-fofbfeicvbpgumafa114te878787878@developer.gserviceaccount.com";  
常量字符串keyFilePath=@“D:\key.p12”;
var证书=新的X509Certificate2(certificateFile,“notasecret”,X509keystrageFlags.Exportable);
var serviceAccountCredential=新serviceAccountCredential(新serviceAccountCredential.Initializer(serviceAccountEmail)
{
范围=范围
}.FromCertificate(证书));
任务任务=((iTokeAccess)serviceAccountCredential).GetAccessTokenForRequestAsync();
task.Wait();
var\u accessToken=task.Result;
ViewBag.Token=\u accessToken;
返回视图();
}
检查我的问题:我希望它能帮助您。