yash
yash

Reputation: 2271

how to solve this 403 io exception?

i want to give whole google drive access from my application to users. and i use following code to authorize from google and oauth2.0.

function OpenGoogleLogin()
              {   
                    var url = "https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive&redirect_uri=http://localhost:8080/Abc/Oauth2callback&response_type=code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxx&access_type=offline&approval_prompt=force";
                    window.location = url;
             }

from the above code i got authorization code. on redirected page i put following servlet code

public class Oauth2callback extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Oauth2callback() {
        super();

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        System.out.println("entering doGet");
        try {
            // get code
            String code = request.getParameter("code");
            // format parameters to post
            String urlParameters = "code="
                    + code
                    + "&client_id=xxxxxxxxxxxxxxxxxxxxx"
                    + "&client_secret=xxxxxxxxxxxxxxxxxx"
                    + "&redirect_uri=http://localhost:8080/Abc/Oauth2callback"
                    + "&grant_type=authorization_code";

            //post parameters
            URL url = new URL("https://accounts.google.com/o/oauth2/token");
            URLConnection urlConn = url.openConnection();
            urlConn.setDoOutput(true);
            OutputStreamWriter writer = new OutputStreamWriter(
                    urlConn.getOutputStream());
            writer.write(urlParameters);
            writer.flush();

            //get output in outputString 
            String line, outputString = "";
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    urlConn.getInputStream()));
            while ((line = reader.readLine()) != null) {
                outputString += line;
            }
            System.out.println(outputString);

            //get Access Token 
            JsonObject json = (JsonObject)new JsonParser().parse(outputString);
            String access_token = json.get("access_token").getAsString();
            System.out.println(access_token);

            //get User Info 
            url = new URL(
                    "https://www.googleapis.com/oauth2/v1/userinfo?access_token="
                            + access_token);
            urlConn = url.openConnection();
            outputString = "";
            reader = new BufferedReader(new InputStreamReader(
                    urlConn.getInputStream()));
            while ((line = reader.readLine()) != null) {
                outputString += line;
            }
            System.out.println(outputString);

            // Convert JSON response into Pojo class
            GooglePojo data = new Gson().fromJson(outputString, GooglePojo.class);
            System.out.println(data);
            writer.close();
            reader.close();

        } catch (MalformedURLException e) {
            System.out.println( e);
        } catch (ProtocolException e) {
            System.out.println( e);
        } catch (IOException e) {
            System.out.println( e);
        }
        System.out.println("leaving doGet");
    }

}

in the above code , google pojo is just a pojo class, but i don't know when i run that servlet, it give me access and refrshtoken.. but with that it gives me error

java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxxxxxxxxxxxx.

Upvotes: 2

Views: 201

Answers (2)

Msp
Msp

Reputation: 2493

You are making the initial authorization request with scope https://www.googleapis.com/auth/drive, and using it's code to call https://www.googleapis.com/oauth2/v1/userinfo.

If you want to get user profile information, you will have to use one of these scopes in your initial request,

  1. https://www.googleapis.com/auth/userinfo.email : View email address
  2. https://www.googleapis.com/auth/userinfo.profile : View basic profile info

So, change your first url to

var url = "https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile&redirect_uri=http://localhost:8080/Abc/Oauth2callback&response_type=code&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxx&access_type=offline&approval_prompt=force";

Upvotes: 1

user207421
user207421

Reputation: 310860

You need to URLEncode the request parameters. A URL isn't valid as a parameter without encoding.

Upvotes: 0

Related Questions