Uploading a csv in box folder is giving 403
i'm new to scripting and was trying to upload a csv file from local to box folder. I followed code used here(https://support.box.com/hc/en-us/community/posts/360049140554-Upload-a-file-using-API-in-PowerShell) i was passing access token in the above code. But it was showing 403. I have also created a custom app in box which has following permissions:
And also added app in admins custom app manager.
Here is my code:
#function that returns the content type based on the file extension
function Get-ContentType
{
param([System.IO.FileInfo]$file = $null);
$contentType = $null;
$contentTypeMap = @{
".csv"= "text/csv";
}
if ( $file )
{
$contentType = $contentTypeMap[$file.Extension.ToLower()];
}
$contentType;
}
#uploads file to box
function uploadFile {
param(
[System.IO.FileInfo] $filename = $null,
[string] $folderid = $null
);
$boundary = [guid]::NewGuid().ToString()
$contenttype = Get-ContentType -file $filename
$headers = @{}
$headers.Add("ServerHost", "https://upload.box.com")
$headers.Add("Authorization", "Bearer $accessToken")
$headers.Add("cache-control","no-cache")
$filebody = [System.IO.File]::ReadAllBytes($filename)
$enc = [System.Text.Encoding]::GetEncoding("utf-8")
$filebodytemplate = $enc.GetString($filebody)
#creating the formdata manually
$template = "
--$boundary
Content-Disposition: form-data; name=""file""; filename="""+$filename.Name+"""
"+$filebodytemplate+"
--$boundary
Content-Type:text/csv; charset=utf-8
Content-Disposition: form-data; name=""metadata""
{""name"":"""+$filename.Name+""",""parent"":{""id"":"""+$folderid+"""}}
--$boundary--"
#mail upload process beins with Invoke-RestMethod
try{
$upload_file = Invoke-RestMethod -Uri "https://upload.box.com/api/2.0/files/content" -Method POST -Headers $headers -Body $template -ContentType "multipart/form-data;boundary=$boundary" -Verbose
if($upload_file.total_count -eq 1){
$size = $upload_file.Entries.size
write-host $upload_file.Entries.name " ($size bytes) uploaded successfully"
}
Write-Host $upload_file
}
catch [Exception] {
Write-Host $_.Exception
$exception = $_.ErrorDetails
switch ($exception.status) {
409 { Write-Host "name exists"}
}
}
}
$ClientId="<Client Id>"
$Secret="<Client Secret>"
$code="<Code from url>"
$Uri = "https://app.box.com/api/oauth2/token"
$Body = @{
grant_type="authorization_code"
client_id = $ClientId
client_secret=$Secret
code=$code
}
$resp = Invoke-RestMethod -Method Post -Uri $Uri -Body $Body -ContentType "application/x-www-form-urlencoded"
#accesstoken
$accessToken = $resp.access_token
#provide file full path and the parent folder ID
uploadFile -filename <path of file to be uploaded> -folderid <folderId>
I might be missing some other permissions.
Any help will be thankfull.
-
Hi Shubhan,
That post is 5 years old.
In the mean time we developed a Box CLI (command line interface) that makes scripting a breeze.
Take a look here, and let us know if it suites your use case. There are some scripting examples, although I count's find one specific for just uploading.
Let us know if this helps.
Post is closed for comments.
Comments
1 comment