2014-08-27 138 views
0

所以我试图让用户使用SLRequest的Facebook个人资料图像。我觉得我已经搜遍了整个互联网无济于事,并在我的智慧结束。这里的窘境......iOS7访问用户Facebook个人资料图片

的代码版本1:

let store = ACAccountStore() 
let type = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook) 
store.requestAccessToAccountsWithType(type, options: [ ACFacebookAppIdKey: "1437725166510606", ACFacebookPermissionsKey: ["email"] ]) { (granted: Bool, error: NSError!) -> Void in 
    if granted { 
     let accounts = store.accountsWithAccountType(type) 
     if let account = accounts.last as? ACAccount { 
      let pictureURLString = "https://graph.facebook.com/v2.1/me/picture" 
      let request = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: NSURL(string: pictureURLString), parameters: nil) 
      request.account = account 
      request.performRequestWithHandler() { (data: NSData!, response: NSHTTPURLResponse!, error: NSError!) -> Void in 
       if let imageData = data { 
        // Save the image 
        // println("Data size: \(imageData.length)\ndata: \(imageData.description)\nAs string: \(NSString(data: imageData, encoding: NSUTF8StringEncoding))") 
        data.writeToFile(NSFileManager.defaultManager().profileImagePath(), atomically: true) 
       } 
      } 
     } 
    } 
} 

好了,所以这个版本的作品,但返回一个非常,非常小版个人资料图片。我想要一个更大的图像!根据Facebook上的文档以及其他许多人的说法,要做到这一点的方法是指定如下参数:type = large或width = 120 & height = 120但只要我这样做,我会得到以下错误:

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}} 

当Facebook的文档用于获取个人资料图片(在https://developers.facebook.com/docs/graph-api/reference/v2.1/user/picture)明确说明:

Because profile pictures are always public on Facebook, this call does not require any access token.

许多建议,比如这个答案https://stackoverflow.com/a/7882628/1175289,使用Facebook ID,而不是“我”的请求建议,但是现在我们得到一个app_scoped_user_id而不是规范FBID。

编辑:这工作正常,我只是一个木板! :)


对于理智的缘故,这里是导致该错误代码:

let store = ACAccountStore() 
    let type = store.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook) 
    store.requestAccessToAccountsWithType(type, options: [ ACFacebookAppIdKey: "1437725166510606", ACFacebookPermissionsKey: ["email"] ]) { (granted: Bool, error: NSError!) -> Void in 
     if granted { 
      let accounts = store.accountsWithAccountType(type) 
      if let account = accounts.last as? ACAccount { 
       let pictureURLString = "https://graph.facebook.com/v2.1/me/picture?type=large" 
       let request = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: NSURL(string: pictureURLString), parameters: nil) 
       request.account = account 
       request.performRequestWithHandler() { (data: NSData!, response: NSHTTPURLResponse!, error: NSError!) -> Void in 
        if let imageData = data { 
         // Save the image 
         // println("Data size: \(imageData.length)\ndata: \(imageData.description)\nAs string: \(NSString(data: imageData, encoding: NSUTF8StringEncoding))") 
         data.writeToFile(NSFileManager.defaultManager().profileImagePath(), atomically: true) 
        } 
       } 
      } 
     } 
    } 

,你可以看到,已经改变的唯一事情是添加型的=大?到url字符串。


如果有人遇到类似的问题,或者有任何想法我做错了什么,帮助将非常感激! :)

回答

1

因为您在API调用中使用/me/,所以需要access_token,因为API不知道谁是me。如果您将其替换为用户标识,例如

https://graph.facebook.com/v2.1/4/picture?type=large 

它应该工作正常。

如果你想继续在URL中使用/me/,只是追加了用户的access_token到URL过,例如:

https://graph.facebook.com/v2.1/4/picture?type=large&access_token=abcdef 
+0

嘿,感谢您的答复。我现在想为我的愚蠢道歉!我曾经认为使用用户ID在使用app_scoped_user_id时不起作用,事实上,我之前刚刚错误地构建了链接!如你所说,它似乎确实有效! – 2014-08-27 16:03:02

+0

你不会有任何想法为什么要求https://graph.facebook.com/v2.1/10204218234286542/picture?type=large在浏览器中正常工作,但是当通过我的应用程序中的SLRequest请求时,返回:{“error”:{“message”:“(#100)type必须是下列值之一:small,normal,large,square”,“type”:“OAuthException”,“code”:100}} ?? – 2014-08-27 16:16:26

+0

@ george-green,您将不得不添加type = large参数作为SLRequest参数的一部分词典例如'参数:@ {@ “类型”:@ “正常”}'。然后它会工作。 – fschaper 2015-03-02 09:12:30

相关问题