gpt4 book ai didi

java - 身份验证 Microsoft OneDrive REST 服务 Java/Ionic 2

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:07 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中通过 REST 服务向 Microsoft OneDrive 进行身份验证,使用 Java 作为后端和 Ionic 2。如果我直接从 Chrome 调用我的服务,身份验证现在可以正常工作。我发布代码:

private static final String REDIRECT_URI = "http://localhost:8080/CloudToCloud/onedrive/getToken";

@RequestMapping(value = { "/getAccess" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void authorizationFlow(HttpServletRequest request, HttpServletResponse response)
throws IOException, InterruptedException {
try {
String authURL = "https://login.live.com/oauth20_authorize.srf?client_id=" + CLIENT_ID
+ "&scope=wl.signin%20wl.basic%20wl.offline_access%20wl.skydrive_update&response_type=code&redirect_uri="
+ REDIRECT_URI;
response.sendRedirect(authURL);
} catch (Exception e) {
logger.severe(e.getMessage());
}
}

@RequestMapping(value = { "/getToken" }, params = { "code" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<JSONObject> getToken(@RequestParam("code") String code)
throws IOException, InterruptedException, ParseException {
JSONObject json = null;
try {
logger.info("Auth CODE: " + code);
String url = "https://login.live.com/oauth20_token.srf";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

String urlParameters = "client_id=" + CLIENT_ID + "&" + "redirect_uri=" + REDIRECT_URI + "&"
+ "client_secret=" + SECRET + "&" + "code=" + code + "&" + "grant_type=authorization_code";
logger.info(url + urlParameters);

con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
logger.info("REQUEST SENT. Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
String risposta = response.toString();
JSONParser parser = new JSONParser();
json = (JSONObject) parser.parse(risposta);
String token = json.get("access_token").toString();

FileWriter file = new FileWriter(DATA_STORE_DIR);
file.write(json.toJSONString());

logger.info("\nTOKEN:\n" + token);

file.flush();
file.close();
return new ResponseEntity<JSONObject>(json, HttpStatus.OK);
} catch (Exception e) {
logger.info("ERRORE: " + e.getMessage());
return new ResponseEntity<JSONObject>(HttpStatus.BAD_REQUEST);
}
}

我调用第一个服务,获得一个代码,然后在第二个服务上进行重定向,该服务为我提供了一个带有 token 的 json,用于所有其他调用。到目前为止一切正常。使用 Ionic 2 出现问题。我发布代码:

服务:

getAuthOneDrive(){
var url = 'http://localhost:8080/CloudToCloud/onedrive/getAccess';
var response = this.http.get(url).map(res => res.json());
return response;
}

组件:

getAuthOneDrive(){
this.cloudServiceAuthentication.getAuthOneDrive().subscribe(
err => {
console.log(err);
},
() => console.log('getAuthOneDrive Complete')
);
}

还有我在 ionic.config.json 中配置的代理:

{
"name": "C2C",
"app_id": "c6203dd8",
"v2": true,
"typescript": true,
"proxies": [
{
"path": "/",
"proxyUrl": "http://localhost:8080/"
}
]
}

如果我尝试通过单击按钮从 Ionic2 中的应用程序调用相同的服务 ( http://localhost:8080/CloudToCloud/onedrive/getAcces ),则会收到此错误。

XMLHttpRequest cannot load http://localhost:8080/CloudToCloud/onedrive/getAccess. Redirect from 'http://localhost:8080/CloudToCloud/onedrive/getAccess' to 'https://login.live.com/oauth20_authorize.srf?client_id=ae9573ba-6bc0-4a87-8…ype=code&redirect_uri=http://localhost:8080/CloudToCloud/onedrive/getToken' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

我真的尝试了一切。如果有人能给我帮助,我将不胜感激。谢谢! ;)

编辑:

这是我尝试做的:1-添加“'Access-Control-Allow-Origin','*' header ,我收到此错误:

XMLHttpRequest cannot load http://localhost:8080/CloudToCloud/onedrive/getAccess. Redirect from 'http://localhost:8080/CloudToCloud/onedrive/getAccess' to 'https://login.live.com/oauth20_authorize.srf?client_id=ae9573ba-6bc0-4a87-8…ype=code&redirect_uri=http://localhost:8080/CloudToCloud/onedrive/getToken' has been blocked by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.

2-在第一个服务中不要使用response.sendRedirect,而是使用HttpsURLConnection或Spring RestTemplate来满足请求;3-尝试直接从 Ionic 调用 Microsoft 服务;4-使用Spring注释@CrossOrigin,但我遇到了同样的错误。

最佳答案

出于安全原因,跨域请求受到浏览器的限制。看看HTTP access control (CORS)了解其工作原理的详细说明。

设置以下 header 应该可以让您解决开发中的问题:

Access-Control-Allow-Origin: *

只需确保在生产部署中删除此 header (或至少将其限制在“*”之外)。

关于java - 身份验证 Microsoft OneDrive REST 服务 Java/Ionic 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41381922/

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