1
votes
  • Language: C#
  • Framework: .NET CORE 1.1.1
  • Nuget: Google.Apis.YouTube.v3 1.25.0.760 and Google.Apis 1.25.0

Hi,

What I try to do

I need to get an access token (with a refresh token) to upload videos on my users YouTube accounts.

What is not working

When I request a token through Google API, it's working in localhost but not in production. In production I get redirect_uri_mismatch error when I redirect user to Google's OAuth server.

How I try to do it

I created a console token in Google API Console because I need to retrieve an offline token (it's not possible to get an offline token if I create an Web Application token on the Google API Console).

In my code : I redirect the user to Google's OAuth server with this url :

https://accounts.google.com/o/oauth2/auth?client_id=XXXXXX.apps.googleusercontent.com&redirect_uri=MyRedirectUri&scope=https://www.googleapis.com/auth/youtube.readonly&response_type=code&access_type=offline

To make things more easy to read :

In development, it's working. In production, it's not working (redirect_uri_mismatch)

enter image description here

I also tried to change redirect_uri to urn:ietf:wg:oauth:2.0:oob like the answer of this post said. But it's just open a page with a code to copy / paste.

PS : I notified that when I download my "client_secrets.json" from Google Console API, there is a redirect_uris in the download file which have these values : ["urn:ietf:wg:oauth:2.0:oob","http://localhost"]. But in the Google API Console I can't modify this parameter.

PS2 : I don't use .NET API to request token because as the answer to this post say (and as I tried previously), getting an offline token with .NET API is not possible.

Thanks for your help

EDIT 1 : This post not answer my question because he use a "Web application token" for one shot request. In my case, I use "Console token" (called "Other" in Google API Console) where I can't set the redirections. I use "Console token" to get an offline token.

2
In the post you mentioned, he use a "Web application token" for one shot request. In my case, I use "Console token" (called "Other" in Google API Console) where I can't set the redirections. I use "Console token" to get an offline token.JohnB

2 Answers

0
votes

In the dev console, you should be using "web application token", and that will allow you to specify http://xxxxxx.azurewebsites.net/Account/Oauth as one of your redirect URLs. Also note that using a plaintext URL is not encouraged, so ideally you should be serving https://xxxxxx.azurewebsites.net/Account/Oauth. In the dev console you can add multiple URLs, so I suggest add both the http and https versions.

0
votes

Just add this

app.Use((context, next) =>
       {
        context.Request.Scheme = "https";
        return next();
       });

on your startup class (Startup.cs) under the "Configure" method.