【问题标题】:Google API Autentication - redirect_uri_mismatch errorGoogle API 身份验证 - redirect_uri_mismatch 错误
【发布时间】:2015-01-24 11:18:54
【问题描述】:

我正在尝试使用Google Plus API 验证我的网络应用程序。在我的Cliend Id for Web Application 的凭据下的Google Developer console 中。

我添加了以下重定向 uri:http://localhost:50883/oauth/add_oauth_token

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

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

所以谷歌出于任何原因将我的重定向 uri 更改为端口 55404,为什么?非常感谢和问候

这是代码:

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文件是这个:

{"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"}}

【问题讨论】:

标签: c# google-api google-oauth google-api-dotnet-client


【解决方案1】:

我只是通过将 DataStore 更改为 null 解决了同样的问题。

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

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

【讨论】:

    【解决方案2】:

    API 重定向 URL 和您的应用程序重定向 URL 应该相同.... 例如(如果您的应用程序 URL 像这样 localhost:55404/authorize/ 并且需要在 API Redirect URL localhost:55404/authorize/ 中添加相同的 URL)

    【讨论】:

    • 我在使用 Google Calendar API v3 时遇到了同样的问题,这不是解决方案,因为 IIS 总是更改端口。
    猜你喜欢
    • 2015-06-03
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2021-06-28
    相关资源
    最近更新 更多