0

My question is similar to this post: How to get an access token without Box’s authorization page

In that post, he asks:

I have been granted access(collaborate) in a folder. What I need is to access the folder daily and fetch files from it. Right now the developer token I generate expires in 1 hour. Is there a way I can get the authorization code without the first leg, which requires a user interface. This way I can refresh the access toke whenever I fetch files.

The highest rated answer from "Skippy Ta" tells me most of what I need to know EXCEPT the following:

How do I authenticate using the developer token and how do I refresh? From the github repo for the HelloWorld sample app (https://github.com/box/box-java-sdk-v2) I downloaded, I see these two steps:

boxClient.authenticate(boxOAuthToken); 

for the initial authentication, and

boxClient.addOAuthRefreshListener(new OAuthRefreshListener() {
    @Override
    public void onRefresh(IAuthData newAuthData) {
        // TODO: Update the stored access token.
    }
});

for the refresh.

I'm having trouble putting all this together. First, the authenticate method does not accept a String boxOAuthToken, it accepts an IAuthData object, whatever that is. So I cannot conduct the initial authentication.

Even if I were to achieve initial authentication, I could not refresh, because I don't know how to access the token once I'm authenticated in order to store it, and if I stored that token as a String, I don't know how to wrap it in the proper object and conduct the update alluded to by the

// TODO: Update the stored access token.

comment above. Thanks for any help you can offer.

Community
  • 1
  • 1

1 Answers1

1

You can take a look at the javafx login UI: https://github.com/box/box-java-sdk-v2/tree/master/BoxJavaFxOAuth

But anyway if you need to build a BoxOAuthToken object from access token and refresh token and authenticate from it, here is what you can do:

HashMap<String, String> tokenMap = new HashMap<String, String>();
tokenMap.put("access_token", access);
tokenMap.put("refresh_token", refresh);
BoxOAuthToken token = new BoxOAuthToken(tokenMap);
boxClient.authenticate(token);

As for the refresh, the sdk auto-refreshes. The only time you need to worry about it is when your app quits and you need to persist the auth. At that point you can save the oauth token out. The refresh listener is used to update the oauth token for you so at the point you need to save oauth out, you have the latest oauth data.

Jian Lin
  • 331
  • 1
  • 5