我正在尝试从 Java webapp 访问 Microsoft LiveConnect。而且我在获取令牌时遇到了问题。
问题如下:我遵循了http://msdn.microsoft.com/en-us/library/live/hh243647.aspx#authcodegrant中所示的授权代码授予流程
LiveConnect 使用以下格式的 URL 将用户代理(浏览器)重定向到我的服务器
http://contoso.com/Callback.htm?code=AUTHORIZATION_CODE
然后我的网络应用程序使用以下格式向 LiveConnect 发出 REST(POST 请求)
POST https://login.live.com/oauth20_token.srf
Content-type: application/x-www-form-urlencoded
client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&client_secret=CLIENT_SECRET&code=AUTHORIZATION_CODE&grant_type=authorization_code
LiveConnect 没有返回一些有意义的数据,而是返回一个包含一些不相关信息的 html 页面,像这样
<html>
...
<body class="modern" onLoad="BodyLoad()">
<div class="header" id="i0272"><span>Microsoft account</span></div>
<div class="content">
<div style="padding:15px 0 0 0;font-size:1px;"> </div>
<h1 class="css0046">We're unable to complete your request</h1>
<p class="css0005">Microsoft account is experiencing technical problems. Please try again later.</p></div>
我还在这里发布了涉及 REST api 的 Java 代码
URL urlConnection = new URL(this.url);
HttpURLConnection connection = (HttpURLConnection) urlConnection.openConnection();
OutputStream outputStream = null;
try {
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setReadTimeout(DEFAULT_READ_TIMEOUT_IN_MS);
connection.setUseCaches(false);
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + data.getBytes("UTF-8").length);
for(String key : params.keySet()){
connection.setRequestProperty(key, params.get(key));
}
executed.set(true);
connection.connect();
outputStream = connection.getOutputStream();
//outputStream.write("\r\n".getBytes("UTF-8"));
outputStream.write(data.getBytes("UTF-8"));
outputStream.flush();
httpInputStream = connection.getResponseCode() != HTTP_OK ?
connection.getErrorStream() :
connection.getInputStream();
return new Response(connection.getResponseCode(), Utils.fromInputStream(httpInputStream));