【问题标题】:How to fetch access token in Google AppEngine (OAuth 2.0) using java如何使用 java 在 Google AppEngine (OAuth 2.0) 中获取访问令牌
【发布时间】:2013-05-30 09:21:49
【问题描述】:

访问令牌套接字 java.lang.RuntimeException: com.google.apphosting.api.ApiProxy$FeatureNotEnabledException:

有没有办法发布兑换令牌的请求(代码)

这是我用于检索代码以访问令牌的代码我正在使用 Httpclient 是任何解决方案

在 index.jsp 中

 response.sendRedirect("https://accounts.google.com/o/oauth2/auth?  scope=https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email&state=%2Fprofile&response_type=code&client_id=158645513564-88hf7od82d09ipr7uiamd540jpd2da1g.apps.googleusercontent.com&redirect_uri=http://gfksgnbgfcv.appspot.com/index2.jsp");

在 index2.jsp 中

<%@page import="com.login.client.TEST"%>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page errorPage="errorPage.jsp" %>
<%
   String code  =  request.getParameter("code");
   String data=TEST.getParseValue(code);
 %>
 <%=code%>
 <%=data %>

还有Java代码

   package com.login.client;


  import java.io.IOException;
  import java.io.UnsupportedEncodingException;

  import java.util.ArrayList;
  import java.util.List;

  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;

  import net.sf.json.JSONObject;
  import net.sf.json.JSONSerializer;
  import net.sourceforge.htmlunit.corejs.javascript.json.JsonParser.ParseException;

  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpException;
  import org.apache.commons.httpclient.HttpStatus;
  import org.apache.commons.httpclient.methods.GetMethod;
  import org.apache.commons.httpclient.methods.PostMethod;
  import org.apache.commons.httpclient.methods.StringRequestEntity;
  import org.apache.commons.io.IOUtils;


   public class TEST  {

 public static String   getParseValue(String code) {
        String token="no token";
        String foros = "code="+code +
                    "&client_id=<YOUR_CLIENT_ID>" +
                    "&client_secret=<YOUR_CLIENT_SECRET>" +
                    "&redirect_uri="+"http://gfksgnbgfcv.appspot.com/index2.jsp" +
                    "&grant_type=authorization_code";
     HttpClient client = new HttpClient();
        String url = "https://accounts.google.com/o/oauth2/token";
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        try {
            post.setRequestEntity(new StringRequestEntity(foros, null, null));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        String accessToken = null;
        try {
            client.executeMethod(post);

            String jsonTxt = IOUtils.toString(post
                    .getResponseBodyAsStream());

            JSONObject json = (JSONObject) JSONSerializer
                    .toJSON(jsonTxt);
            String resp = post.getResponseBodyAsString();

            token = (String) json.getString("access_token");
        } catch (HttpException e) {
            token=e.getMessage();
            throw new RuntimeException(e);
        } catch (IOException e) {
            token=e.getMessage();
            throw new RuntimeException(e);
        } catch (Exception e) {
            token=e.getMessage();
            throw new RuntimeException(e);
        }
     return token;

      }

}

【问题讨论】:

    标签: google-app-engine oauth-2.0 google-oauth google-openid


    【解决方案1】:

    GAE 不支持 Apache HttpClient(至少不支持tweaks)。你应该使用URLFetch API

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2015-05-05
    • 2011-02-28
    • 2019-09-10
    • 2023-01-23
    • 2012-08-03
    相关资源
    最近更新 更多