【问题标题】:Google Oauth 2.0 - redirect_uri mismatch despite being identical to an authorized redirect uri in Google Cloud PlatformGoogle Oauth 2.0 - 尽管与 Google Cloud Platform 中的授权重定向 uri 相同,但 redirect_uri 不匹配
【发布时间】:2020-09-03 20:14:31
【问题描述】:

我在许多其他帖子中看到了这个问题,但没有一个解决了我的问题。我有一个前端 Vue.js 应用程序和一个 Spring Boot Java 应用程序。

我正在使用 vue-google-oauth 提示 Google 从我的前端应用程序登录以获取身份验证代码,然后我想使用我的后端服务器获取用户详细信息并在那里处理逻辑。

在 Google Cloud Platform 上,我定义了一个授权重定向 URI:

当我在前端发送我的身份验证代码时,我使用的是相同的 uri

import api from "@/assets/js/api";
import AdminNavigation from "./AdminNavigation";
import { mapGetters } from "vuex";
import Axios from "axios";

export default {
  name: "Dashboard",
  computed: {
    ...mapGetters(["IsSignedIn"]),
  },
  data() {
    return {
      title: "Christopher s' portfolio admin",
      appDescription:
        "Here you can add contents for the front end portfolio website.",
      isInit: false,
    };
  },
  components: {
    AdminNavigation,
  },
  methods: {
    signIn: async function () {
      try {
        const authCode = await this.$gAuth.getAuthCode();
        Axios.post("http://localhost:8080/authenticate", {
          code: authCode,
          redirect_uri: "http://localhost:3000/admin/dashboard",
        });
      } catch (err) {
        console.log(err);
      }
    },
  },
  mounted() {
    let that = this;
    let checkGauthLoad = setInterval(function () {
      that.isInit = that.$gAuth.isInit;
      if (!this.IsSignedIn) {
        that.signIn();
      }

      if (that.isInit) clearInterval(checkGauthLoad);
    }, 1000);
  },
};

我的后端服务器接收到与 Google Cloud Platform 上定义的相同的身份验证代码和 redirect_uri。

package com.salay.christophersalayportfolio.controllers;

import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.salay.christophersalayportfolio.general.ConstantVariables;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;

@Controller
public class AdminController {
    @CrossOrigin(origins = "http://localhost:3000")
    @RequestMapping(value = "/authenticate", method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    @ResponseBody
    public String authentication(HttpEntity<String> data) throws IOException, ParseException {

        JSONParser parser = new JSONParser();
        JSONObject json = (JSONObject) parser.parse(data.getBody());

        String authCode = json.get("code").toString();
        String redirect_uri = json.get("redirect_uri").toString();

        try {
            GoogleTokenResponse response =
                new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
                    ConstantVariables.GOOGLE_CLIENT_ID,
                    ConstantVariables.GOOGLE_CLIENT_SECRET,
                    authCode, redirect_uri).execute();

            System.out.println("Access token: " + response.getAccessToken());


        } catch (TokenResponseException e) {
            if (e.getDetails() != null) {
                System.err.println("Error: " + e.getDetails().getError());
                if (e.getDetails().getErrorDescription() != null) {
                    System.err.println(e.getDetails().getErrorDescription());
                }
                if (e.getDetails().getErrorUri() != null) {
                    System.err.println(e.getDetails().getErrorUri());
                }
            } else {
                System.err.println(e.getMessage());
            }
        }
        return "";
    }

}

但我收到以下错误:

 400 Bad Request redirect_uri_mismatch          

我一直在查看很多堆栈溢出问题,但到目前为止没有任何解决方案适合我......有什么想法吗?

【问题讨论】:

    标签: java spring-boot vue.js oauth-2.0 google-oauth


    【解决方案1】:

    听起来您没有发送您认为的 OAuth 详细信息。您是否从 Spring Boot 后端捕获到授权服务器的 HTTPS 消息 - 您可以在此处发布详细信息吗?

    如果有帮助,this blog post of mine 包含一些关于在 Java 中配置 HTTP 代理的说明。

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 2017-11-28
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 2014-05-05
      • 1970-01-01
      • 2019-05-09
      相关资源
      最近更新 更多