2014-08-27 141 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

相關問題