Skip to main content

Hi, Please let me solution.


I created a program in nodejs to upload the following file, but it is "Unsupported Media Type".


The program is able to get the number of files, etc.
I think the token is correct.


Is there something I am using wrong in the upload section?


 


const fs = require('fs')
const crypto = require('crypto')
const jwt = require('jsonwebtoken')
const axios = require('axios')
const querystring = require('querystring');

const config = JSON.parse(
  fs.readFileSync('config.json')
)

let run = async () => {
    // In node we don't need to manually decrypt the
  // key, as the JWT library can handle this for us
  let key = {
    key: config.boxAppSettings.appAuth.privateKey,
    passphrase: config.boxAppSettings.appAuth.passphrase
  }

 // We will need the authenticationUrl  again later,
  // so it is handy to define here
  const authenticationUrl = 'https://api.box.com/oauth2/token'

  let claims = {
    'iss': config.boxAppSettings.clientID,
    'sub': config.enterpriseID,
    'box_sub_type': 'enterprise',
    'aud': authenticationUrl,
    // This is an identifier that helps protect against
    // replay attacks
    'jti': crypto.randomBytes(64).toString('hex'),
    // We give the assertion a lifetime of 45 seconds 
    // before it expires
    'exp': Math.floor(Date.now() / 1000) + 45
  }

 let keyId = config.boxAppSettings.appAuth.publicKeyID

// Rather than constructing the JWT assertion manually, we are 
  // using the jsonwebtoken library.
  let assertion = jwt.sign(claims, key, {
    // The API support "RS256", "RS384", and "RS512" encryption
    'algorithm': 'RS512',
    'keyid': keyId,
  })

 // We are using the excellent axios package 
  // to simplify the API call
  let accessToken = await axios.post(
    authenticationUrl, 
    querystring.stringify({
      // This specifies that we are using a JWT assertion
      // to authenticate
      grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      // Our JWT assertion
      assertion: assertion,
      // The OAuth 2 client ID and secret
      client_id: config.boxAppSettings.clientID,
      client_secret: config.boxAppSettings.clientSecret
    })
  )
  // Extract the access token from the API response
  .then(response => response.data.access_token)

// フォルダのID(フォルダ番号)を指定
  const folderID = '12345'; // フォルダのIDを指定
  // ファイルのアップロード
  const uploadUrl = `https://upload.box.com/api/2.0/files/content?folder_id=${folderID}`;
  const fileData = {
    name: 'package.json', // アップロードするファイルの名前
    // content_type: 'text/plain',
    content_type: 'application/json',
  };
  const filePath = 'package.json'; // アップロードするファイルのパス

 let data = await axios.get(
    'https://api.box.com/2.0/folders/12345', {
    headers: { 'Authorization' : `Bearer ${accessToken}` }
  }).then(response => response.data)

 try {
    const response = await axios.post(uploadUrl, fs.createReadStream(filePath), {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        'Content-Type': fileData.content_type,
      }
    });
    console.log('File upload successful:', response.data);
  } catch (error) {
    console.error('File upload failed:', error.response);
  }
}

run()

 


 


 


 

Hi there,


Welcome to Box Community and glad to help!


It shows that you're currently working with a member of our team. Please keep an eye out and we'll stay in touch. 


Thanks for posting!


Reply