如何从后端访问Google OAuth2登录API?

时间:2018-09-11 15:07:05

标签: java api google-signin

我正在尝试在服务器端实现Google登录,而我正在调用的API是 https://accounts.google.com/o/oauth2/auth。如果我直接从浏览器中调用它,它将使用代码作为请求参数将我重定向到我的redirect_uri,但是如果我从后端作为rest webservice运行它,尽管响应为200,它也不会做任何事情

后端代码:

public void oauth2Service(){
    OkHttpClient client = new OkHttpClient();
    StringBuilder oauth2GetCodeUrl = new StringBuilder();
    oauth2GetCodeUrl.append(env.getProperty("oauth2_url"));
    oauth2GetCodeUrl.append("scope=").append(getGoogleSignInProperties.getScope());
    oauth2GetCodeUrl.append("&redirect_uri=").append(getGoogleSignInProperties.getRedirect_uri());
    oauth2GetCodeUrl.append("&response_type=").append(getGoogleSignInProperties.getResponse_id());
    oauth2GetCodeUrl.append("&client_id=").append(getGoogleSignInProperties.getClient_id());
    oauth2GetCodeUrl.append("&approval_prompt=").append(getGoogleSignInProperties.getApproval_prompt());

    Request request = new Request.Builder()
      .url(oauth2GetCodeUrl.toString()).get()
      .addHeader("content-type", "application/x-www-form-urlencoded")
      .addHeader("cache-control", "no-cache")
      .build();

    try {
        Response response = client.newCall(request).execute();
        System.out.println(response.toString());

    } catch (IOException e) {

        e.printStackTrace();
    }
}

api应该将我重定向到请求中已经提到的重定向URI

响应字符串为:Response {protocol = http / 1.1,code = 200,message = OK,url = {https://accounts.google.com/ServiceLogin?passive=1209600&continue=https://accounts.google.com/o/oauth2/auth?scope%3Demail%26redirect_uri%3Dhttp://localhost:8080/MovieSearch/webapi/googleOAuthCallback%26response_type%3Dcode%26client_id%3D52424919169-remgpb2h4nhdua57plc1cem4p0qo4if8.apps.googleusercontent.com%26approval_prompt%3Dforce%26from_login%3D1%26as%3DhYtl9Ra4jWxbygJmS2uAiw&followup=https://accounts.google.com/o/oauth2/auth?scope%3Demail%26redirect_uri%3Dhttp://localhost:8080/MovieSearch/webapi/googleOAuthCallback%26response_type%3Dcode%26client_id%3D52424919169-remgpb2h4nhdua57plc1cem4p0qo4if8.apps.googleusercontent.com%26approval_prompt%3Dforce%26from_login%3D1%26as%3DhYtl9Ra4jWxbygJmS2uAiw&oauth=1&sarp=1&scc=1}

0 个答案:

没有答案