首页 文章

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

提问于
浏览
0

我的应用程序使用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 "Displaying a preview of a OneDrive item" - 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 / /缩略图/ /

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

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

2 回答

  • 0

    您需要调用以下API:

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

    此调用需要使用授权并将重定向返回到 cache-safe 缩略图位置 . 然后,您可以使用此新网址为未经身份验证的用户提供缩略图 .

    例如

    请求:

    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 Headers 中的链接访问缩略图而无需登录 . 仅当文件内容发生更改时,此URL才会更改 .

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

  • 0

    我刚想通了 . 它基于Microsoft本文中的信息...

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

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

    GET / me / drive / items / / children?$ expand =缩略图

    基本上,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!
    }
    

    干杯,安德烈亚斯

相关问题