Alfredo Torre
Alfredo Torre

Reputation: 728

c# web service Google Analytics

I tried to implement a web service in C# to retrieve data from Google Analytics. When I try it on my local machine it works, but when I publish it to the server I have an exception. I created a project on Google Analytics console, and I downloaded the resulting client_secret json. I uploaded the json on the root path of my application and I set the Build Action on the client_secret.json file to Content and the Copy to Output Directory to Copy if newer. This is the code in which I perform authentication:

static async Task<UserCredential> GetCredential()
    {
        try
        {
            string jsonPath = HttpContext.Current.Server.MapPath("~/client_secret.json");

            HttpContext.Current.Response.Write(jsonPath);

            using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
            {
                const string loginEmailAddress = "[email protected]";
                return await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { AnalyticsReportingService.Scope.Analytics },
                    loginEmailAddress, System.Threading.CancellationToken.None,
                    new FileDataStore("GoogleAnalyticsApiWebService")
                    );
            }

        }
        catch (Exception ex)
        {

            HttpContext.Current.Response.Write("\n" + ex.Message);
            return null;
        }
    }

When I try to execute in local it works correctly, when I publish on the server and call it from the UTL I have an exception:

Access to the path 'GoogleAnalyticsApiWebService' is denied.Google.Apis.Requests.RequestError Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.

Someone can help me? Thanks

Upvotes: 0

Views: 182

Answers (2)

Alfredo Torre
Alfredo Torre

Reputation: 728

I Think I solved the freezing problem of the GoogleWebAuthorizationBroker.AuthorizeAsync following answer by iedoc in this thread

Upvotes: 0

Alfredo Torre
Alfredo Torre

Reputation: 728

The problem was about the access permissions for the FileDataStore. Probably specifying a relative path the app was trying to create and write a folder in a position where it didn't had the permissions to write. I changed The path in a different location, that I passed in the variable pathFileDataStore:

static async Task<UserCredential> GetCredential()
    {
        try
        {
            string jsonPath = System.Configuration.ConfigurationManager.AppSettings["VPJSON"].ToString() + "\\client_secret.json";

            HttpContext.Current.Response.Write("JSON PATH: " + jsonPath);

            using (var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
            {
                const string loginEmailAddress = "[email protected]";
                HttpContext.Current.Response.Write("\n EMAIL LOGIN: " + loginEmailAddress + "\n");

                GoogleClientSecrets clientSecrets = GoogleClientSecrets.Load(stream);
                HttpContext.Current.Response.Write("\n" + "CLIENT SECRET: " + clientSecrets.Secrets.ClientSecret + "\n");
                HttpContext.Current.Response.Write("\n" + "CLIENT ID: " + clientSecrets.Secrets.ClientId + "\n\n");

                string pathFileDataStore = "d:\\GADataStore";
                FileDataStore ds = new FileDataStore(pathFileDataStore,true);
                HttpContext.Current.Response.Write("\n" + "PATH FILE DATA STORE: " + ds.FolderPath + "\n");



                return await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    clientSecrets.Secrets,
                    new[] { AnalyticsReportingService.Scope.Analytics },
                    loginEmailAddress, System.Threading.CancellationToken.None,
                    ds
                    );
            }

        }
        catch (Exception ex)
        {

            HttpContext.Current.Response.Write("\n" + "GetCredential() " + ex.Message);
            return null;
        }
    }

Now it creates the folder, but now I have a different problem. It doesn't create any file in the folder. When I call the Url of my Web Service I have this exception: System.Web.HttpException: Request timed out.

Upvotes: 0

Related Questions