身份验证 Microsoft OneDrive REST 服务 Java/Ionic 2
Authentication Microsoft OneDrive REST services Java/Ionic 2
我正在尝试在我的应用程序中通过 REST 服务向 Microsoft OneDrive 进行身份验证,使用 Java 作为后端和 Ionic 2。如果我直接从 [=42= 调用我的服务,身份验证现在可以工作].
我post代码:
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);
}
}
我调用了第一个服务,我得到了一个代码,然后我在第二个服务上进行了重定向,它为我提供了一个 json 和令牌,用于所有其他调用。到目前为止,一切正常。
使用 Ionic 2 时出现问题。我 post 代码:
服务:
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或者SpringRestTemplate来满足请求;
3- 尝试直接从 Ionic 调用 Microsoft 服务;
4-使用Spring注解@CrossOrigin,但我得到了同样的错误。
Cross-origin 出于安全原因,请求被浏览器限制。查看 HTTP access control (CORS) 以详细了解其工作原理。
设置以下 header 应该可以让您解决开发中的问题:
Access-Control-Allow-Origin: *
只需确保在生产部署中删除此 header(或至少将其限制在“*”之外)。
我通过修改第一个服务 /getAccess,添加 header Access-Control-Allow-Origin 并使用我的 REDIRECT_URI 解决了问题,而不再是出现问题的 sendRedirect。
@CrossOrigin(origins = "*")
@RequestMapping(value = { "/getAccess" }, method = RequestMethod.GET)
public void authenticate() throws IOException {
try {
OneDriveSDK sdk = OneDriveFactory.createOneDriveSDK(CLIENT_ID, SECRET, REDIRECT_URI,
OneDriveScope.READWRITE);
String url = sdk.getAuthenticationURL();
logger.info(url);
openWebpage(url);
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Access-Control-Allow-Origin", "*");
con.setDoOutput(true);
int responseCode = con.getResponseCode();
logger.info("REQUEST SENT. Response Code : " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}
我正在尝试在我的应用程序中通过 REST 服务向 Microsoft OneDrive 进行身份验证,使用 Java 作为后端和 Ionic 2。如果我直接从 [=42= 调用我的服务,身份验证现在可以工作]. 我post代码:
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);
}
}
我调用了第一个服务,我得到了一个代码,然后我在第二个服务上进行了重定向,它为我提供了一个 json 和令牌,用于所有其他调用。到目前为止,一切正常。 使用 Ionic 2 时出现问题。我 post 代码:
服务:
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或者SpringRestTemplate来满足请求; 3- 尝试直接从 Ionic 调用 Microsoft 服务; 4-使用Spring注解@CrossOrigin,但我得到了同样的错误。
Cross-origin 出于安全原因,请求被浏览器限制。查看 HTTP access control (CORS) 以详细了解其工作原理。
设置以下 header 应该可以让您解决开发中的问题:
Access-Control-Allow-Origin: *
只需确保在生产部署中删除此 header(或至少将其限制在“*”之外)。
我通过修改第一个服务 /getAccess,添加 header Access-Control-Allow-Origin 并使用我的 REDIRECT_URI 解决了问题,而不再是出现问题的 sendRedirect。
@CrossOrigin(origins = "*")
@RequestMapping(value = { "/getAccess" }, method = RequestMethod.GET)
public void authenticate() throws IOException {
try {
OneDriveSDK sdk = OneDriveFactory.createOneDriveSDK(CLIENT_ID, SECRET, REDIRECT_URI,
OneDriveScope.READWRITE);
String url = sdk.getAuthenticationURL();
logger.info(url);
openWebpage(url);
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Access-Control-Allow-Origin", "*");
con.setDoOutput(true);
int responseCode = con.getResponseCode();
logger.info("REQUEST SENT. Response Code : " + responseCode);
} catch (Exception e) {
e.printStackTrace();
}
}