I am debugging a console application that uses Oauth2 credentials, and the service provider requires that I specify a redirect URI. I specify the redirect URI in the service provider's Web portal. The service is Google Analytics.
I have successfully tested the service with a Node.js application. In the service's configuration, I have specified a redirect URI of http://localhost:8080
. The Node application actually starts on port 8080 by default, so I can simply run http-server
, browse to http://localhost:8080
, call the service, and see the response with data.
However, when I try to call the same service with a C# console application, I receive an HTTP 400 error in the browser. The error states:
The redirect URI in the request, http://localhost:59291/authorize/, does not match the ones authorized for the OAuth client.
Each time I try to run the application, the port changes. Here is the code (referenced from Analytics Reporting API V4 Client Library for .NET):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Google.Apis.AnalyticsReporting.v4;
using Google.Apis.AnalyticsReporting.v4.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.Threading;
namespace Google_Analytics_API_Test
{
class Program
{
static void Main(string[] args)
{
try
{
var credential = GetCredential().Result;
using (var svc = new AnalyticsReportingService(
new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Google Analytics API Console"
}))
{
var dateRange = new DateRange
{
StartDate = "2017-04-24",
EndDate = "today"
};
var sessions = new Metric
{
Expression = "ga:sessions",
Alias = "Sessions"
};
var date = new Dimension { Name = "ga:date" };
var reportRequest = new ReportRequest
{
DateRanges = new List<DateRange> { dateRange },
Dimensions = new List<Dimension> { date },
Metrics = new List<Metric> { sessions },
ViewId = "<<viewID>>"
};
var getReportsRequest = new GetReportsRequest
{
ReportRequests = new List<ReportRequest> { reportRequest }
};
var batchRequest = svc.Reports.BatchGet(getReportsRequest);
var response = batchRequest.Execute();
foreach (var x in response.Reports.First().Data.Rows)
{
Console.WriteLine(string.Join(",", x.Dimensions) +
" " + string.Join(", ", x.Metrics.First().Values));
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static async Task<UserCredential> GetCredential()
{
using (var stream = new FileStream("client_secret.json",
FileMode.Open, FileAccess.Read))
{
const string loginEmailAddress = "<<emailAddress>>";
return await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { AnalyticsReportingService.Scope.Analytics },
loginEmailAddress, CancellationToken.None,
new FileDataStore("GoogleAnalyticsApiConsole"));
}
}
}
}
I have searched for a way to configure IIS Express with a static port for debugging, but I'm not sure that IIS is even involved here - rather than just something like TcpClient / TcpListener
objects that are implemented by the Google Analytics library?
Is there a way to specify a port for async / await requests in a C# .NET console application?