OAuth my god, how the f*** do I get this to work?! (Google APIs with .NET web app)

My experience (as a n00b) with trying to get Google OAuth tokens using a .NET MVC web app.

I know that you can create a web app in Visual Studio and have the user authenticate using their Google account, but what if you wanted to request more access (more ‘scope’ as its called)?

I started Googling for resources and the results weren’t too satisfactory, which is why I’m writing this.

Now, I understand the basics of OAuth, but the thing is I wanted to use the APIs as opposed to raw HTTP requests. Before using the APIs though, I would recommend looking at how to do OAuth with raw HTTP requests first because the APIs hide a lot of the inner workings. [Official Google documentation]

I eventually figured out that you have to create the website project without authentication, then use something called FlowMetadata (Google API) — there are probably other ways to do it. but this is what I chose.

Apparently when you host on Azure (which I have yet to try yet), the FileDataStore won’t work because it references the local file system. So you have to do this:

Now in your controller, you can set up a method as so:

The following method I show corresponds to: http://localhost:1234/Home/IndexAsync

You can see how it uses ‘AuthorizationCodeMvcApp’ with the AppFlowMedadata (described in the Google .NET API link above)

If you set a breakpoint after the “result”, you can see the access token (which expires after X minutes) and the refresh token (which can be exchanged for new access tokens). Once you get the credentials, it is easy to use the Google services (just import the appropriate Nuget modules) — there’s a ton of links on that so I won’t cover it here.

Here’s AppFlowMetaData (snippet from the Google .NET API link):

This took me like 3 and a half hours to figure out, I hope you can get it working quicker now that you’ve read this!

Was this helpful?

If you want to buy me lunch, you can do so here — as a way of saying thanks, I’ll share the .sln and I can help you troubleshoot it when I get time.


Please ❤ the article if you’ve ever been stuck between validation and moving forward!

If you want to follow my project, you can do so here:

Like what you read? Give Ambrose a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.