Getting error with a HTTP Post request?

2019-09-16 14:55发布

问题:

I am trying create a new share link for onedrive file access.

This is what I am supposed to do,

POST /drive/items/{item-id}/action.createLink
Content-Type: application/json

{
"type": "view"
}

I have created a post request in ruby as follow,

require 'net/http'
require 'json'

uri = URI.parse("https://api.onedrive.com/v1.0")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new("https://api.onedrive.com/v1.0/drive/items/file.88e469b2d4c51142.88E469B2D4C51142!113/action.createLink")
req.content_type = "application/json"   
json = {:type=> "view"}.to_json   
req.body = json
response = http.request(req)
puts response.body
puts response

By running the above code I am getting the following error as JSON,

{
"error": {
"code": "invalidArgument",
"message": "ObjectHandle is Invalid",
"innererror": {
  "code": "badArgument",
  "innererror": {
    "code": "invalidObjectHandle",
    "innererror": {
      "code": "invalidResourceId"
    }
  }
}
}
}

My first impression is that I am setting the request.body with a wrong value of JSON how to overcome this error?

回答1:

The id you provided (file.88e469b2d4c51142.88E469B2D4C51142!113) is not a valid id for this API. It looks suspiciously like one obtained from the LiveConnect API, and those aren't compatible with this new API. If you obtain the id using the new api and use that with your same query it should work.