1
  • 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.

Community
  • 1
  • 1
JohnB
  • 49
  • 1
  • 10
  • 1
    Possible duplicate of [How to add multiple redirect URIs for Google OAuth 2?](http://stackoverflow.com/questions/23717452/how-to-add-multiple-redirect-uris-for-google-oauth-2) – CBroe Apr 27 '17 at 08:51
  • 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 Apr 27 '17 at 08:58

2 Answers2

0

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.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Hi, thanks for your answer. If I use a "web application token" I will not be able to get an offline_token. And I need an offline token to upload videos on my users account. – JohnB Apr 27 '17 at 15:15
  • @JohnB yes you can get an offline refresh token from a "web application credential" – pinoyyid Apr 27 '17 at 18:38
  • The last time I tried to get an offline token with a "web application credential" I get an error saying that I can't. I'm gonna try again. If there is an error I will update my post. Thanks – JohnB Apr 27 '17 at 19:56
  • I just tried to get an offline token with "web application credential" and it works. I just have a problem when I try to refresh it, but it's not the same topic. Thanks – JohnB May 01 '17 at 06:48
0

Just add this

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

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

DavidRomo
  • 141
  • 3