Hey all! Thanks in advanced.
I am trying to write a powershell script to get an offline refresh token so I can do unattended uploads.
The problem is that I have to authenticate for an authorization code manually each time it is ran. How do I make it unattended? Here is the code:
$client_id = "CLIENTID"
$client_secret = "CLIENTSECRET"
# Set up the OAuth2 authorization URL
#$scope = "offline"
#$auth_url = "{0}?response_type=code&client_id={1}&scope={2}" -f $auth_url, $client_id, $scope
# Open the authorization URL in the default web browser and prompt the user to sign in and authorize your app
Start-Process $auth_url
Write-Host "Please sign in and authorize your app in the browser window that just opened."
# Prompt the user to enter the authorization code provided by Dropbox after they have authorized your app
$authorization_code = Read-Host "Enter the authorization code provided by Dropbox"
# Set up the access token request parameters
$body = {
code = $authorization_code
grant_type = "authorization_code"
client_id = $client_id
redirect_uri = $redirectUri
client_secret = $client_secret
}
# Send the access token request and parse the response for the access token and refresh token
$response = Invoke-RestMethod -Method Post -Uri $token_url -Body $body
$access_token = $response.access_token
$refresh_token = $response.refresh_token
# Use the access token to make API calls to Dropbox
# For example, you can list the contents of the root folder like this:
$headers = {
"Authorization" = "Bearer $access_token"
}
$list_body = {
path = ""
recursive = $false
}
$list_response = Invoke-RestMethod -Method Post -Uri $list_url -Headers $headers -Body ($list_body | ConvertTo-Json)
$list_response.entries | Select-Object name, path_display
# Output the access token and refresh token to the console
Write-Host "Your access token is: $access_token"
Write-Host "Your refresh token is: $refresh_token"