Microsoft Live Connect,REST访问问题

时间:2013-04-23 12:39:07

标签: liveconnect

我正在尝试从Java webapp访问Microsoft LiveConnect。我有一个获得令牌的问题。

问题如下:我按照http://msdn.microsoft.com/en-us/library/live/hh243647.aspx#authcodegrant

中所示的授权代码授予流程进行了操作

LiveConnect使用以下格式的网址将用户代理(浏览器)重定向到我的服务器

http://contoso.com/Callback.htm?code=AUTHORIZATION_CODE

然后我的web-app以下列格式向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;">&nbsp;</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));

1 个答案:

答案 0 :(得分:1)

包含您的duff内容的网页的网址可能更有意义。我有这个:

https://login.live.com/err.srf?lc=2057#error=invalid_request&error_description=The%20provided%20value%20for%20the%20input%20parameter%20%27redirect_uri%27%20is%20not%20valid.%20The%20expected%20value%20is%20%27https://login.live.com/oauth20_desktop.srf%27%20or%20a%20URL%20which%20matches%20the%20redirect%20URI%20registered%20for%20this%20client%20application.&state=redirect_type%3dauth%26display%3dpage%26request_ts%3d1412608929255%26response_method%3dcookie%26secure_cookie%3dfalse

确保您的应用和OneDrive开发人员中心重定向网址都具有完全相同的网址,即:

  

http://contoso.com/Callback.htm

包括页面。

当我在OneDrive开发人员中心的网址未包含该网页时,我得到了相同的平淡错误页面,其中包含上面的网址。 将重定向网址设置为:

https://account.live.com/developers/applications

登录并登录仪表板&gt;我的应用&gt; YourAppName&gt; API设置

开发时,您还需要将域(contoso.com)重定向到本地IIS IP地址(127.0.0.1),如下所述: “http://msdn.microsoft.com/en-us/library/live/hh826547.aspx

希望这有助于某人(我知道这已经很晚了)