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 = "my@emailaddress.com";
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