Google OAuth 2 错误:redirect_uri_mismatch 随机 url 参数 ASP.NET

Dan*_*nko 1 asp.net-mvc google-api oauth-2.0

我已经通过以下模板在我的网站中通过 VK、Instagram、Facebook 进行了身份验证。但是谷歌需要“重定向网址”。我的重定向网址是这样的:

http://localhost:4588/main/AuthenticationCallback?__provider__=google%2B&__sid__=6f3cc5957e4742758719f9b7decc2c09
Run Code Online (Sandbox Code Playgroud)

参数“ sid ”每次都是随机的。所以我不能给谷歌准确的网址。我试图http://localhost:4588/main/AuthenticationCallback像我在 Instagram 上所做的那样输入,它对 Instagram 有效,但谷歌一直向我显示“400 错误:redirect_uri_mismatch”

我还尝试将http://localhost:4588/main/AuthenticationCallback授权 url 中的 URL 参数作为 URL 参数传递给下面的 google。但在这种情况下,根本不调用方法“IAuthenticationClient.RequestAuthentication”。

你能告诉我我应该为我的 Google 应用输入什么作为“重定向 URL”吗?

使用 OAuth2 的模板类:

public class GoogleAuthenticationClient : IAuthenticationClient
{
    public string appId;
    public string appSecret;
    private string redirectUri;

    public GoogleAuthenticationClient(string appId, string appSecret)
    {
        this.appId = appId;
        this.appSecret = appSecret;
    }

    string IAuthenticationClient.ProviderName
    {
        get { return "google+"; }
    }

    void IAuthenticationClient.RequestAuthentication(HttpContextBase context, Uri returnUrl)
    {
        var APP_ID = this.appId;
        this.redirectUri = context.Server.UrlEncode(returnUrl.ToString());

        var address = String.Format(
                "https://accounts.google.com/o/oauth2/auth?client_id={0}&redirect_uri={1}&response_type=code&scope={2}",
                APP_ID, this.redirectUri, "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email"
            );

        HttpContext.Current.Response.Redirect(address, false);
    }

    class AccessToken
    {
        public string access_token = null;
        public string user_id = null;
    }

    class UserData
    {
        public string uid = null;
        public string first_name = null;
        public string last_name = null;
        public string photo_50 = null;
    }

    class UsersData
    {
        public UserData[] response = null;
    }

    AuthenticationResult IAuthenticationClient.VerifyAuthentication(HttpContextBase context)
    {
        try
        {
            string code = context.Request["code"];

            var address = String.Format(
                    "https://accounts.google.com/o/oauth2/token?client_id={0}&client_secret={1}&code={2}&redirect_uri={3}",
                    this.appId, this.appSecret, code, this.redirectUri);

            var response = GoogleAuthenticationClient.Load(address);
            var accessToken = GoogleAuthenticationClient.DeserializeJson<AccessToken>(response);

            address = String.Format(
                    "https://www.googleapis.com/plus/v1/people/{0}?access_token=1/fFBGRNJru1FQd44AzqT3Zg",
                    accessToken.user_id);

            response = GoogleAuthenticationClient.Load(address);
            var usersData = GoogleAuthenticationClient.DeserializeJson<UsersData>(response);
            var userData = usersData.response.First();

            return new AuthenticationResult(
                true, (this as IAuthenticationClient).ProviderName, accessToken.user_id,
                userData.first_name + " " + userData.last_name,
                new Dictionary<string, string>());
        }
        catch (Exception ex)
        {
            return new AuthenticationResult(ex);
        }
    }

    public static string Load(string address)
    {
        var request = WebRequest.Create(address) as HttpWebRequest;
        using (var response = request.GetResponse() as HttpWebResponse)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                return reader.ReadToEnd();
            }
        }
    }

    public static T DeserializeJson<T>(string input)
    {
        var serializer = new JavaScriptSerializer();
        return serializer.Deserialize<T>(input);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器中的代码:

    public void ExternalLogin(string provider)
    {
        OAuthWebSecurity.RegisterClient(
            client: new GoogleAuthenticationClient(
                    "APP_ID", "APP_CODE"),
            displayName: "google+", // ??????? ?? ??????
            extraData: null);

        ExternalLoginCallback(provider);
    }

    public void ExternalLoginCallback(string provider)
    {
        OAuthWebSecurity.RequestAuthentication(provider, Url.Action("AuthenticationCallback"));
    }

    public ActionResult AuthenticationCallback()
    {
        var result = OAuthWebSecurity.VerifyAuthentication();

        if (result.IsSuccessful == false)
        {
            return null;
        }
        else
        {
            var provider = result.Provider;
            var uniqueUserID = result.ProviderUserId;
            return RedirectToAction("Main", "Main");
        }            
    }
Run Code Online (Sandbox Code Playgroud)

ome*_*rio 5

您可以按照下面的说明授权重定向 URI,但您不能向重定向 uri 添加任何参数,请参阅此答案,了解如何使用多个参数将参数传递给 Google google oauth2 redirect_uri

在 Google Cloud Console 上创建客户端(“APP_ID”、“APP_CODE”)时需要设置授权重定向 URI。只需导航到您的项目的 API 控制台并编辑 Web 客户端以设置您想要使用的正确重定向 URI。

在此处输入图片说明