如何解决这个 403 io 异常?
how to solve this 403 io exception?
我想将我的应用程序的整个 google 驱动器访问权授予用户。
我使用以下代码从 google 和 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;
}
从上面的代码我得到了授权码。
在重定向的页面上,我放置了以下 servlet 代码
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");
}
}
在上面的代码中,google pojo 只是一个 pojo class,但我不知道我什么时候 运行 那个 servlet,它给了我访问权限和 refrshtoken..
但它给了我错误
java.io.IOException:服务器返回 HTTP 响应代码:403 URL:https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxxxxxxxxxxxx。
您需要URL对请求参数进行编码。 URL 在没有编码的情况下作为参数是无效的。
您正在发出范围为 https://www.googleapis.com/auth/drive
的初始授权请求,并使用其代码调用 https://www.googleapis.com/oauth2/v1/userinfo
。
如果您想获取用户个人资料信息,您必须在初始请求中使用这些范围之一,
- https://www.googleapis.com/auth/userinfo.email : 查看电子邮件地址
- https://www.googleapis.com/auth/userinfo.profile : 查看基本资料信息
因此,将您的第一个 url 更改为
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";
我想将我的应用程序的整个 google 驱动器访问权授予用户。 我使用以下代码从 google 和 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;
}
从上面的代码我得到了授权码。 在重定向的页面上,我放置了以下 servlet 代码
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");
}
}
在上面的代码中,google pojo 只是一个 pojo class,但我不知道我什么时候 运行 那个 servlet,它给了我访问权限和 refrshtoken.. 但它给了我错误
java.io.IOException:服务器返回 HTTP 响应代码:403 URL:https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxxxxxxxxxxxx。
您需要URL对请求参数进行编码。 URL 在没有编码的情况下作为参数是无效的。
您正在发出范围为 https://www.googleapis.com/auth/drive
的初始授权请求,并使用其代码调用 https://www.googleapis.com/oauth2/v1/userinfo
。
如果您想获取用户个人资料信息,您必须在初始请求中使用这些范围之一,
- https://www.googleapis.com/auth/userinfo.email : 查看电子邮件地址
- https://www.googleapis.com/auth/userinfo.profile : 查看基本资料信息
因此,将您的第一个 url 更改为
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";