如何将共享OneDrive文件的缩略图提供给未经授权的用户?旧API中提供的这个有用的OneDrive功能现已被破坏

时间:2016-03-09 17:25:45

标签: onedrive

我的应用使用OneDrive API功能让未经授权的用户使用旧的API请求获取共享OneDrive文件的缩略图:

https:// apis.live.net/v5.0/skydrive/get_item_preview?type=normal&url=[shared_link_to_OneDrive_file]

此功能现已中断(任何此类链接都会返回XMLHttpRequest连接错误0x2eff)。 我的Windows应用商店应用无法再提供此功能。

任何人都可以尝试检查它,链接到共享的OneDrive文件:

https://onedrive.live.com/redir?resid=AABF0E8064900F8D!27202&authkey=!AJTeSCuaHMc45eY&v=3&ithint=photo%2cjpg

链接到共享OneDrive文件的预览图像(根据旧的OneDrive API “显示OneDrive项目的预览” - https:// msdn.microsoft.com/en-us/library/jj680723.aspx):

https://apis.live.net/v5.0/skydrive/get_item_preview?type=normal&url=https%3A%2F%2Fonedrive.live.com%2Fredir%3Fresid%3DAABF0E8064900F8D!27202%26authkey%3D!AJTeSCuaHMc45eY%26v%3D3%26ithint%3Dphoto%252cjpg 生成错误:SCRIPT7002:XMLHttpRequest:网络错误0x2eff

СurrentOneDriveAPI缩略图功能:

GET / drive / items / {item-id} /缩略图/ {thumb-id} / {size}

仅供授权用户使用,无法为未经授权的用户提供共享OneDrive文件的缩略图访问

Windows应用商店应用如何让未经授权的用户使用当前的OneDrive API获取共享OneDrive文件(视频等)的缩略图? 有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您需要调用以下API:

GET /drive/items/{item-id}/thumbnails/{thumb-id}/{size}/content

此通话需要使用授权并将重定向返回到缓存安全缩略图位置。然后,您可以使用此新网址为未经身份验证的用户提供缩略图。

e.g。

请求:

GET https://api.onedrive.com/v1.0/drive/items/D094522DE0B10F6D!152/thumbnails/0/small/content
Authorization: bearer <access token>

响应:

HTTP/1.1 302 Found
Location: https://qg3u2w.bn1302.livefilestore.com/y3m1LKnRaQvGEEhv_GU3mVsewg_-aizIXDaVczGwGFIqtNcVSCihLo7s2mNdUrKicuBnB2sGlSwMQTzQw7v34cHLkchKHL_5YC3IMx1SMcpndtdb9bmQ6y2iG4id0HHgCUlgctvYsDrE24XALwXv2KWRUwCCvDJC4hlkqYgnwGBUSQ

您现在可以使用Location标题中的链接访问缩略图而无需登录。只有当文件内容发生变化时,此网址才会更改。

您可以在文档here中阅读更多内容。

答案 1 :(得分:0)

我只是想通了。它基于Microsoft提供的本文中的信息...

https://docs.microsoft.com/en-ca/onedrive/developer/rest-api/api/driveitem_list_thumbnails?view=odsp-graph-online

...查看“列出DriveItem时获取缩略图”部分。它显示了来自诸如以下调用的相关JSON返回结构:

获取/ me / drive / items / {item-id} / children?$ expand = thumbnails

基本上,JSON返回结构为您提供每种缩略图格式的字符串URL。然后,您创建URLSession来上传这些URL(一旦将它们从String转换为URL)

以下是使用Swift(Apple)的代码摘录:

////////////////////////////////////////////////////////////////////////////////
//
// Download a thumbnail with a URL and label the URLSession with an ID.
//
func downloadThumbnail(url: URL, id: String) {

    // Create a URLSession. This is an object that controls the operation or flow
    // control with respect to asynchronous operations. It sets the callback delegate
    // when the operation is complete.
    let urlSession: URLSession = {
        //let config = URLSessionConfiguration.default
        let config = URLSessionConfiguration.background(withIdentifier: id)
        config.isDiscretionary = true
        config.sessionSendsLaunchEvents = true
        //config.identifier = "file download"
        return URLSession(configuration: config, delegate: self as URLSessionDelegate, delegateQueue: OperationQueue.main)
    }()

    // Create the URLRequest. This is needed so that "Authorization" can be made, as well
    // as the actual HTTP command. The url, on initialization, is the command... along
    // with the "GET" setting of the httpMethod property.
    var request = URLRequest(url: url)

    // Set the Authorization header for the request. We use Bearer tokens, so we specify Bearer + the token we got from the result
    request.setValue("Bearer \(self.accessToken)", forHTTPHeaderField: "Authorization")
    request.httpMethod = "GET"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    // This initiates the asynchronous data task
    let backgroundTask = urlSession.downloadTask(with: request)
    //backgroundTask.earliestBeginDate = Date().addingTimeInterval(60 * 60)
    backgroundTask.countOfBytesClientExpectsToSend = 60
    backgroundTask.countOfBytesClientExpectsToReceive = 15 * 1024
    backgroundTask.resume()

}

...当然,您需要具有正确的“ accessToken”(如上所示),但是您还必须为URLSession编写通用的回调函数,即:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
                didFinishDownloadingTo location: URL) {

    Swift.print("DEBUG: urlSession callback reached")

    // This was the identifier that you setup URLSession with
    let id = session.configuration.identifier

    // "location" is the temporary URL that the thumbnail was downloaded to
    let temp = location

    // You can convert this URL into any kind of image object. Just Google it!
}

干杯,安德里亚斯