必须使用活动访问令牌来查询有关当前用户的信息。 Facebook Graph API

时间:2017-07-10 08:10:56

标签: ios facebook-graph-api

我想将用户的照片从脸部取到我的应用。我正在使用图形API表单。在参数中,我也传递了有效的访问令牌。

但我收到错误:

"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "no-store";
"Content-Encoding" = gzip;
"Content-Length" = 157;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Mon, 10 Jul 2017 08:01:35 GMT";
Expires = "Sat, 01 Jan 2000 00:00:00 GMT";
Pragma = "no-cache";
Vary = "Accept-Encoding";
"Www-Authenticate" = "OAuth \"Facebook Platform\" \"invalid_request\" \"An active access token must be used to query information about the current user.\"";

代码:

func fetchPhotos(_ accessToken : String)
{

    let url = NSURL(string : "https://graph.facebook.com/me/photos")
    let request = NSMutableURLRequest(url: url as! URL)
    request.httpMethod = "GET"
    let params = ["access_token" : accessToken]
    do
    {
        request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])

    }
    catch

    {
        print("Error")
    }
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in

   print(response)

    });
    task.resume()

}

我如何获取accessToken

func getAccessToken(completion : @escaping (_ atoken : String?) -> ())
{
    let url = NSURL(string : "https://graph.facebook.com/oauth/access_token?client_id=9621*******9&client_secret=********40a2abeae6****a199&grant_type=client_credentials")
    let request = NSMutableURLRequest(url: url as! URL)
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
        do
        {
            let resultDict =  try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
            let accessToken = (resultDict as! [String : String])["access_token"]!
            completion(accessToken)
        }
        catch
         {
            completion(nil)
        }
    });
    task.resume()


}

1 个答案:

答案 0 :(得分:0)

看起来您正在使用应用访问令牌。改为使用用户访问令牌。