Swift:Facebook Graph API不接受有效的access_token

问题描述 投票:0回答:1

我正在从mac上的命令行运行一个快速脚本。我只是想尝试发布一个我验证过的access_token的url在其他浏览器中运行。 Facebook不接受这个帖子。我在swift中使用URLSession而不是Facebook SDK。

guard let url = URL(string: "https://graph.facebook.com/aumuaamata?") else { return }
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)

var request = URLRequest(url: url)
request.httpMethod = "POST"

let postString = "access_token=433263852003213|gLKD8g4ZAlADEDSSKqy8u_KZ_0"
request.httpBody = postString.data(using: .utf8)

request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

let runLoop = CFRunLoopGetCurrent()
let task = session.dataTask(with: request, completionHandler: { data, response, error in
     if error == nil {
         print("it works!")
     } else {
         print("task error: \(error!)")
     }
     CFRunLoopStop(runLoop)
})
task.resume()
CFRunLoopRun()

Facebook用A page access token is required to request this resourceinsufficient_scope回应。

我尝试将access_token添加到URL,然后对其进行编码,如下所示:

guard let escapedUrlString = urlRawString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else { return }

但是脚本不会运行并且说它不是有效的URL。输入浏览器一切正常。

有任何想法吗?

swift facebook-graph-api http-status-code-403
1个回答
0
投票

这是修复。我用curl尝试了它,并遇到了同样的问题。

request.httpMethod = "GET"
request.setValue("Bearer 433263852003213|gLKD8g4ZAlADEDSSKqy8u_KZ_0", forHTTPHeaderField: "Authorization")

似乎很奇怪,这是必要的,但它的工作原理!

不,我发布的示例令牌无效(可能)。

© www.soinside.com 2019 - 2024. All rights reserved.