gpt4 book ai didi

java - 如何解决这个 403 io 异常?

转载 作者:行者123 更新时间:2023-11-30 10:50:31 24 4
gpt4 key购买 nike

我想从我的应用程序向用户提供整个谷歌驱动器访问权限。我使用以下代码从 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 类,但我不知道当我运行那个 servlet 时,它会给我访问权限和 refrshtoken..但它给了我错误

java.io.IOException:服务器返回 HTTP 响应代码:403 URL:https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxxxxxxxxxxxx .

最佳答案

您正在使用范围 https://www.googleapis.com/auth/drive 发出初始授权请求,并使用它的代码调用 https://www.googleapis。 com/oauth2/v1/用户信息

如果您想获取用户个人资料信息,您必须在初始请求中使用这些范围之一,

  1. https://www.googleapis.com/auth/userinfo.email : 查看电子邮件地址
  2. https://www.googleapis.com/auth/userinfo.profile : 查看基本资料信息

因此,将您的第一个网址更改为

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";

关于java - 如何解决这个 403 io 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35046107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com