You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
rdyar
11 months agoHelpful | Level 5
Simple Node script not working with clientId /secret
I'm trying to do the worlds simplest node dropbox script but getting an error, clearly I am missing something. I set up an App - and gave it all the read permissions. Then I have this script ...
- 11 months ago
I was able to get this all to work as a script. So far it has worked great, running every 15 minutes for the last week. I only do the dropbox auth part if the thing I want to share has actually changed which happens several times per day during working hours.
In order get the refresh token chatGPT give me a script to run in powershell:
I pasted this in all at once:
$clientId = "yourAppIDHere"$clientSecret = "YourAppSecret"$authorizationCode = "ACCESSCodeFromPreviousStep"$base64Auth = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${clientId}:${clientSecret}"))$headers = @{ Authorization = "Basic $base64Auth"}$body = @{ code = $authorizationCode grant_type = "authorization_code"}$response = Invoke-RestMethod -Uri "https://api.dropbox.com/oauth2/token" -Method Post -Headers $headers -Body $body# Output the access token$response.refresh_tokenThat gave me refresh token that I can then use to get a current access token - so that is hard coded in the script.I then have a db auth function which uses that refresh token and returns a new access token:import fetch from "node-fetch";const clientId = "yourAppID";const clientSecret = "YourAppSecret";const refreshToken ="RefreshTokenFromPowershell";// Function to refresh the access tokenexport default async function refreshAccessToken() {method: "POST",headers: {"Content-Type": "application/x-www-form-urlencoded",Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`,},body: `grant_type=refresh_token&refresh_token=${refreshToken}`,});const data = await response.json();console.log("data in refresh :>> ", data);if (data.access_token) {return data.access_token;} else {throw new Error("Failed to refresh access token");}}Then I use that access token like you normally would.import fetch from "node-fetch";const config = {fetch,accessToken,clientId,clientSecret,};const dbx = new Dropbox(config);const data = await dbx.filesListFolder({ path: "" });
Здравко
Legendary | Level 20
rdyar, Probably you're using some custom version of the command. This tool is OS agnostic and is used in the same way on every OS and that's why preferred when examples are provided (aside of its powerful support for almost all recent protocols versions and all their varieties). The documentation can be seen here. Download the official version to be able follow all examples here (not only mine). The '-u' parameter provides data for authentication (basic by default - the one used by Dropbox) and is shorthand of --user, Yes. Authentication can be performed once only. As alternative you can use -H or --header parameter with value explicitly representing the basics authentication header. Something like 'Authorization: Basic <credentialsVal>'. Here credentialsVal should be represented with base64 encoded value of -u parameter '<App key>:<App secret>' (together with the colon in the middle).
PS: Data shouldn't be pre-encoded in any way! So using --data-urlencode may make parameters invalid. Such parameter pre-encoding is only useful when you want to pass something that may break the used encoding, so need to be pre-encoded (i.e. saned). Here is no such a thing.
rdyar
11 months agoHelpful | Level 5
I was able to get this all to work as a script. So far it has worked great, running every 15 minutes for the last week. I only do the dropbox auth part if the thing I want to share has actually changed which happens several times per day during working hours.
In order get the refresh token chatGPT give me a script to run in powershell:
I pasted this in all at once:
$clientId = "yourAppIDHere"
$clientSecret = "YourAppSecret"
$authorizationCode = "ACCESSCodeFromPreviousStep"
$base64Auth = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${clientId}:${clientSecret}"))
$headers = @{ Authorization = "Basic $base64Auth"}
$body = @{ code = $authorizationCode grant_type = "authorization_code"}
$response = Invoke-RestMethod -Uri "https://api.dropbox.com/oauth2/token" -Method Post -Headers $headers -Body $body
# Output the access token
$response.refresh_token
That gave me refresh token that I can then use to get a current access token - so that is hard coded in the script.
I then have a db auth function which uses that refresh token and returns a new access token:
import fetch from "node-fetch";
const clientId = "yourAppID";
const clientSecret = "YourAppSecret";
const refreshToken =
"RefreshTokenFromPowershell";
// Function to refresh the access token
export default async function refreshAccessToken() {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${Buffer.from(
`${clientId}:${clientSecret}`
).toString("base64")}`,
},
body: `grant_type=refresh_token&refresh_token=${refreshToken}`,
});
const data = await response.json();
console.log("data in refresh :>> ", data);
if (data.access_token) {
return data.access_token;
} else {
throw new Error("Failed to refresh access token");
}
}
Then I use that access token like you normally would.
import fetch from "node-fetch";
const config = {
fetch,
accessToken,
clientId,
clientSecret,
};
const dbx = new Dropbox(config);
const data = await dbx.filesListFolder({ path: "" });
- Greg-DB11 months agoDropbox Staff
rdyar If you're using an official Dropbox SDK, like the official Dropbox JavaScript SDK you're using in the last piece of code you shared, you don't need to perform the "grant_type=refresh_token" step yourself. If you pass the necessary credentials (refresh token and app key/secret) to the SDK, it will automatically handle that refresh process for you.
Also, when using filesListFolder, be sure to implement filesListFolderContinue as well. Check out the documentation there for more information.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,882 PostsLatest Activity: 3 years agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!