Google API Autentication - redirect_uri_mismatch错误 [英] Google API Autentication - redirect_uri_mismatch error

查看:881
本文介绍了Google API Autentication - redirect_uri_mismatch错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用 Google Plus API 验证我的网络应用程序。在我的 Cliend ID的Credentials下的 Google开发者控制台中为Web应用程序

I'm trying to authenticate my web app with Google Plus API. In the Google Developer console under Credentials for my Cliend Id for Web Application.

我添加下面的redirect uri: http:// localhost:50883 / oauth / add_oauth_token

I add the following redirect uri: http://localhost:50883/oauth/add_oauth_token

当我运行我的应用程序时,出现以下错误:

When I run my application I get the following Error:

400. That’s an error.

Error: redirect_uri_mismatch

Application: SID3

You can email the developer of this application at: carlosmoralesdiego@gmail.com

The redirect URI in the request: http://localhost:55404/authorize/ did not match a registered   redirect URI.

Learn more

Request Details
from_login=1
response_type=code
scope=https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email    https://www.googleapis.com/auth/userinfo.profile
access_type=offline
redirect_uri=http://localhost:55404/authorize/
as=-45fed094242eac62
pli=1
client_id=389029010035-knoo3a2445r77pirc06h8fhgdc5q0dsv.apps.googleusercontent.com
authuser=0
hl=es

因此,出于任何原因,google会将我的redirect uri更改为端口55404,为什么?非常感谢和问候

So for any reason google changes my redirect uri to the port 55404, why?. Thanks so much and regards

这是代码:

This is the code:

protected void LoginToGooglePlus(object sender, ImageClickEventArgs e)
    {

        PlusService service = null;
        UserCredential credential = null;

            string[] scopes = new string[] {PlusService.Scope.PlusLogin, PlusService.Scope.UserinfoEmail,
 PlusService.Scope.UserinfoProfile};
            // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
           try
        {
            // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
            UserCredential credentials;
            using (var stream = new FileStream("C:\\Users\\Usuario\\Documents\\Visual Studio 2010\\Projects\\WebApplication1\\WebApplication1\\client_secret.json", FileMode.Open, FileAccess.Read))
            {

                credentials= GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, scopes, Environment.UserName, CancellationToken.None, new FileDataStore("WebApplication1")).Result;
            }
        }
        catch (Exception ex)
        {
            //If the user hits cancel you wont get access.
            if (ex.InnerException.Message.IndexOf("access_denied") != -1)
            {
                Console.WriteLine("User declined access");
                Console.ReadLine();
                return;
            }
            else
            {
                Console.WriteLine("Unknown Authentication Error:" + ex.Message);
                Console.ReadLine();
                return;
            }
        }

        // Now we create a Google service. All of our requests will be run though this.
        service = new PlusService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "SID3",
        });




}

我使用它的json文件就是这个:

The json file which I use it's this one:

{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"jpkVDaaMjlLCHGh67DJ9Zv19","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"389029010035-knoo3a2445r77pirc06h8fhgdc5q0dsv@developer.gserviceaccount.com","redirect_uris":["http://localhost:50880/Paneles.aspx"],"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/389029010035-knoo3a2445r77pirc06h8fhgdc5q0dsv@developer.gserviceaccount.com","client_id":"389029010035-knoo3a2445r77pirc06h8fhgdc5q0dsv.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}


推荐答案

我只是通过更改DataStore

I just solved the same problem simply by changing DataStore to null.

credentials= GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    scopes,
    Environment.UserName,
    CancellationToken.None,
    null // <-- here
    ).Result;

我不知道为什么,但它阻止了随机端口shenanigans。
我还应该补充说,就我而言,它只是解决了一个问题,因为我需要在线发布我的应用程序,而这个解决方案恰恰是错误的。如果任何人需要在线版本(asp.net 4.5 MVC 5),您可以使用此处的代码。

I don't know why but it stops the random port shenanigans. I should also add, that in my case, it just solved one problem since I needed to publish my app online, where this solution was just wrong. If anyone needs the online version (asp.net 4.5 MVC 5) you can use the code here.

这篇关于Google API Autentication - redirect_uri_mismatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆