我正在使用 Box SDK 来获取这样的访问令牌:
val MAX_CACHE_ENTRIES = 100
val accessTokenCache: IAccessTokenCache = new InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES)
val boxConfig: BoxConfig = {
// Read Box config file
val stream = getClass.getResourceAsStream( path )
val reader = new InputStreamReader( stream )
BoxConfig.readFrom( reader )
}
val connection: BoxDeveloperEditionAPIConnection = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, accessTokenCache)
val accessToken = connection.getAccessToken()
然后使用此访问令牌和文件 ID 我发送 POST 以读取文件,如下所示:
val fileId = "FILE ID"
val url = s"https://api.box.com/2.0/files/$fileId/content"
val header = List(Header("Authorization", s"Bearer $token"))
// send request
当我用这个自动生成令牌发送 POST 请求时,我得到一个 404 File Not Found 错误,但如果我从 Box Developer Console 手动生成一个令牌,同样的代码可以工作,我可以检索文件内容。
响应看起来像这样
{
"access_token":"TOKEN",
"expires_in":4195,
"restricted_to":[],
"token_type":"bearer"
}
我使用的访问令牌有误吗?
我设法使用共享链接读取文件,如下所示:
val enterpriseApi = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, accessTokenCache)
// Read File Metadata
val sharedLink = "......"
val info = BoxItem.getSharedItem(enterpriseApi, sharedLink)
println(s"File ID: ${info.getID}, Owener ID: ${info.getOwnedBy().getID} and name ${info.getOwnedBy().getName}")
// Create a connection as the File owner
val api = BoxDeveloperEditionAPIConnection.getAppUserConnection(info.getOwnedBy().getID, boxConfig)
println(s"enterprise token ${client.connection.getAccessToken}, user token is ${api.getAccessToken}")
// Read file as user
val file: BoxFile = new BoxFile(api, info.getID)
val output = new ByteOutputStream()
file.download(output)
println(new String(output.getBytes))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句