Q: How to retrieve the "code" returned by Google OAuth authentication from the HTTP header in ASP.Net MVC Account Controller? And is this the right approach to retrieve a user's Google profile photo?
I am trying to access the Google People API to retrieve an authenticated user's cover photo. I am using ASP.Net MVC 5 with the Owin Identity 'Account Controller'. I can authenticate OK with Google and register a new user in my ASP.net Users table (LocalDB).
But in order to retrieve the user's cover photo during the ExternalLoginConfirmation, I am trying (and failing) to retrieve the "code" querystring that is returned to my callback - see Chrome Dev Tools snapshot below.
I had assumed that it would be found in the QueryStrings property within the HttpContext of the AccountController but I can't find the "code" key or value when debugging the following Controller properties in breakpoints in the ExternalLoginCallback and ExternalLoginConfirmation actions:
var httpContext = HttpContext;
var modelState = ModelState;
var request = Request;
var response = Response;
var routeData = RouteData;
var session = Session;
var owinContext = HttpContext.GetOwinContext();
I'm still feeling out how the flow works - so I could be coming at this from the wrong angle so open to suggestions. I hope to use this code directly in a call to the Google People API (as the "key=" in the query string). Or perhaps I send the code to the Google authentication API to get an access token which I can then use as above. Or I could be misunderstanding entirely.
My intended URI to hit the Google People API for the user's cover photo looks like this:
string uri = $@"https://people.googleapis.com/v1/people/{UserId}?personFields=coverPhotos&fields=coverPhotos&key={token}";
(Where token is either the code returned when the user is authenticated by Google or a subsequent access token from an additional call, which uses the initial authentication code).
