سؤال

I am trying to update email alias for different users. I am able to authenticate, get the code and then get the access token. I am sending the access token in the HTTP POST request as a Header. I am using Java & Apache HTTPClient to make the RESTful call. Here is the code snippet (Only relevant code shown).

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);
            }
        }
    }

The problem is that the response I get is some HTML code that says "The page you were viewing has expired. Please go back and try your request again." I was expecting some JSON string.

What I am doing incorrect? In the Post request instead of sending the email address I used the user id. But I get the same error.

In fact when I try to fetch the email alias of a user using the HTTP GET request I get an error "Not Found". The user does exist. I have an admin control. I can see them.

Thanks Raj

هل كانت مفيدة؟

المحلول 2

I was using the NameValuePair instead of the JSON string that was being expected. So I removed the following

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

and added

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

and then things started to work!

نصائح أخرى

Try a get on /users to get the array of all your users in the enterprise first. Is that working for you? If not, can you do a get on /users/me? If you can't get the former, then your API key may not have the "manage an enterprise" grant setup for it. You have to set that up in the app management, where you setup your OAuth2 callback URL.

Not sure why you are getting HTML back. That usually only happens on badly formed requests that our servers can't even parse, like you are hitting the wrong URL.

Just a reminder, OAuth2 URL is different from the API URL. 1st is https://www.box.com/api/oauth2/.... 2nd is https://api.box.com/2.0/...

As for setting the Email alias, that's entirely possible, once you know the ID of the user you are trying to set the alias for. Documentation is here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top