Box API更新电子邮件别名问题

时间:2014-03-17 14:32:12

标签: java apache-httpclient-4.x box-api

我正在尝试更新不同用户的电子邮件别名。我能够进行身份验证,获取代码然后获取访问令牌。我将HTTP POST请求中的访问令牌作为标头发送。我正在使用Java& Apache HTTPClient用于进行RESTful调用。以下是代码段(仅显示相关代码)。

if (httpClient != null) {

        String apiURL = getApiURL();
        apiURL = MessageFormat.format(apiURL, "firstname.lastname@company.com");
        // apiURL = https://api.box.com/2.0/users/firstname.lastname@company.com/email_aliases
        // firstname.lastname@company.com does exist in the Box Account

        HttpPost post = new HttpPost(apiURL);
        post.addHeader("Authorization", "Bearer "+accessToken);

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();            
        nameValuePairs.add(new BasicNameValuePair("email", "updateemail@company.com"));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Charset.defaultCharset()));

        HttpEntity entity = post.getEntity();

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseFromBox = httpClient.execute(post, responseHandler);
        writeResponse(response, responseFromBox);
        if (responseFromBox != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("apiURL-->"+apiURL);
                logger.debug(responseFromBox);
            }
        }
    }

问题是,我得到的回复是一些HTML代码,上面写着“您正在查看的页面已过期。请返回并再次尝试您的请求。”我期待一些JSON字符串。

我做错了什么?在Post请求中,我使用了用户ID,而不是发送电子邮件地址。但是我得到了同样的错误。

事实上,当我尝试使用HTTP GET请求获取用户的电子邮件别名时,我收到错误“Not Found”。用户确实存在。我有一个管理控制。我可以看到它们。

由于 拉吉

2 个答案:

答案 0 :(得分:1)

尝试使用get / users来首先获取企业中所有用户的数组。这对你有用吗?如果没有,你可以做一个get / users / me吗?如果您无法获得前者,那么您的API密钥可能没有&#34;管理企业&#34;授予它的设置。您必须在应用管理中进行设置,您可以在其中设置OAuth2回调网址。

不确定为什么要回复HTML。这通常只发生在我们的服务器甚至无法解析的错误形成的请求上,就像您遇到错误的URL一样。

提醒一下,OAuth2网址与API网址不同。第一个是https://www.box.com/api/oauth2/ ....第二个是https://api.box.com/2.0/ ...

至于设置电子邮件别名,一旦您知道要为其设置别名的用户的ID,那就完全可以了。文档为here

答案 1 :(得分:0)

我使用的是NameValuePair而不是预期的JSON字符串。所以我删除了以下

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();            
nameValuePairs.add(new BasicNameValuePair("email", "updateemail@company.com"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Charset.defaultCharset()));

并添加了

String json = "{\"email\":\"firstname.lastname@company.com\"}";
StringEntity entity = new StringEntity(json, Charset.defaultCharset());
post.setEntity(entity);

然后事情就开始起作用了!

相关问题