Stack Exchange
How to fetch access token in Google AppEngine (OAuth 2.0) using java
I am getting exception for accessing token Socket java.lang.RuntimeException: com.google.apphosting.api.ApiProxy$FeatureNotEnabledException: 
Is there any solution to post the request (code) for exchange of token
Here is my code for retrieving code to access token i am using Httpclient  is any solution 
in 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");

in 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 %>

And Java code 
   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;

      }

}

Apache HttpClient is not supported on GAE (at least not without tweaks). You should use URLFetch API.

Hi i am using URLFetch API i am getting {"error":"invalid_request"} my java code is here http://pastebin.com/2TferLyB what wrong with this code ?

You must not set Content-length, this is done by GAE automatically.

Also this is a POST - you must create a x-www-form-urlencoded body, not add parameters into header.

It is working fine...but getting timeout error often {Timeout while fetching URL: https://accounts.google.com/o/oauth2/token}....any suggestion?

setDeadline(..): https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/urlfetch/FetchOptions#setDeadline(java.lang.Double)

I am using provisioning api by  com.google.gdata.data.appsforyourdomain jar now i am setting token for credential and using this token getting some error Here is my code to retrive All users whta is wrong here http://pastebin.com/z4gYub9G
