Skip to main content

Good morning,


I have a long running process that starts by reading the directory contents of between 70k + 700k files, when running the process on a very small set of data, it works 100%, on my side of the process it then runs a series of ‘things’ on that data, then generates a metadata template and stamps it on the file. This works 100% until, what I think is, the token becoming stale/invalid/expired. I say this because if this process runs for like 4-6hrs, nothing. It works perfect, but around 24hr, when i stamp the template i get 401 errors.



So the first thing i did (my program is in Rust) is re-build my auth before stamping the templates.


Example Code:


let client = Client::builder().build().unwrap();


let params = s


(“client_id”, “”),


(“client_secret”, “”),


(“grant_type”, “client_credentials”),


(“box_subject_type”, “enterprise”),


(“box_subject_id”, “534608”),


];


//Get client token


let response = client


.post(“https://api.box.com/oauth2/token”)


.form(&params)


.send()


.await


.unwrap();


let t = response.json::().await.unwrap();



Then what happens is i have a client object and when i go to write, I grab the client and insert the token:


let response = &self


.client


.post(&url)


.bearer_auth(&self.token.access_token)


.header(header::CONTENT_TYPE, “application/json”)


.body(final_body.clone())


.send()


.await


.unwrap();



So what I did is, basically call this code again:


let response = client


.post(“https://api.box.com/oauth2/token”)


.form(&params)


.send()


.await


.unwrap();


let t = response.json::().await.unwrap();


which returns a token the same as when i started (which works of course to read the folders and files) and then try to write the templates…



I get 401 errors.


sWriting template to file: 1553933437399 …Template for file: 1553933437399 Error: 401 IsInformational ?: false IsClientError? : true]



When i look at documenation for Templates there is no listed 401 error:







Even weirder is, 3 weeks ago I ran this program AS IS, on a 220k folder and it ran to completion, this entire 401 issue only started in last 2 weeks (ish) range, did something change?



So I guess my questions are:





  1. With Client Credentials Grant how long is the token valid for.


  2. Why re-authing does the template stamp not work, while all the other API calls do.


  3. Why did this sudden start/change? What has changed to this process?




Please any help would be great as my project deadline is very close and this ‘was working’



Thank you,


Andrew

Hi @AndrewC



So cool to see you using Rust and the Box API, we actually started a Rust SDK, but it didn’t go anywhere.



Anyway this is surprising, any access token, JWT, CCG, OAuth, Developer, they all last for 60 minutes.



So just before 60 minutes elapse, you should get a new one.



What I do not understand is how it works for 20+ hours without complaining, I must be missing something.



Typically, when using one of our official SDKs, they take care of refreshing the access token, and it some folks have reported to have issues in rare situation of a single request taking more than one hour.



Is that your case?



Let us know.


Yes that very cool about your Rust SDK, i looked at it when building my own. What I ended up doing is exsactly that, every x minutes do the refresh on the token, and my issue is solved.



Now only if Rust had a SetInterval like JS >:)



Thanks,


Andrew


Awesome, let us know if you decide to publish the your Rust interface to Box API.



If I may suggest, all our SDKs are open source, you can take a look at any of them and see how Box implements the token refresh.



https://github.com/orgs/box/repositories?q=sdk



Best regards


Very cool, I don’t think what I built would be good for sharing , as its mostly internal business logic and a slim Box Wrapper.



Yah, as per my initial comment, it wasn’t throwing 401’s (with exact same code base) like 2 weeks ago, and some of these process ran for days and days. Honestly if there is a log on your side of all the stuff and tokens i have used/generated on your platform I would dig into it.



When I started getting the 401’s i immediately figured it was expiring tokens, and the fix was actually pretty trivial, but this was working before 2 weeks ago! LOL



If you need more info, let me know, if you wanna chalk it up to wierd hiccup im also ok with that, knowing they last 1hr was more than enough 🙂


Thank you ,


Andrew


Well if you change your mind or if you want to write an article about creating a thin Box client in Rust, let us know, we would gladly publish that on our blog.







To your 401 point, I personally do not have access to the logs, but I will alert the support team about this. They might ping you though.



Cheers


This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.


Reply