Abolfazl
Abolfazl

Reputation: 1700

ServiceCredential.cs not found in googleAnalyticApi

i tried to retrieve data from google analytic api, and here is my class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.Apis.Analytics.v3;
using Google.Apis.Auth.OAuth2;
using System.Threading;
using Google.Apis.Util.Store;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Services;

namespace zirfoon.model
{
    public class googleanalyze
    {
public static void main(){
    string[] scopes = new string[] { AnalyticsService.Scope.Analytics,AnalyticsService.Scope.AnalyticsManageUsers }; // view and manage your Google Analytics data

    var keyFilePath = HttpContext.Current.Server.MapPath("/loominexMvc-f104810a4caa.p12");    // Downloaded from https://console.developers.google.com
    var serviceAccountEmail = "[email protected]";  // found https://console.developers.google.com

    //loading the Key file
    var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
    var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes =scopes
    }.FromCertificate(certificate));

}
    }
} 

when i get to line below

var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
    Scopes =scopes 
}.FromCertificate(certificate));

visual studio debugger opens a windows to select ServiceCredential.cs but it does not exist,i changed scope but still nothing... i have created my api account correctly and all of the information such as serviceAccountEmail and keyFilePath is correct but i still get error

Upvotes: 0

Views: 90

Answers (2)

Abolfazl
Abolfazl

Reputation: 1700

I found out that problem was only for debugging mode When I ran project without debugger there was not any issue...

Upvotes: 0

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117146

Your code is a little different then what I normally use.

Authentication

string[] scopes = new string[] {AnalyticsService.Scope.Analytics}; // view and manage your Google Analytics data

var keyFilePath = @"c:\file.p12" ;    // Downloaded from https://console.developers.google.com
var serviceAccountEmail = "[email protected]";  // found https://console.developers.google.com

//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) {
                                                   Scopes = scopes}.FromCertificate(certificate));

Create the service

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

I would double check that your mapping to keyFilePath is correct.

Code ripped from my tutorial Google Analytics API authentcation

Upvotes: 1

Related Questions