2014-08-27 4 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) 
       } 
      } 
     } 
    } 
} 

좋아, 그래서이 버전은 작동하지만 프로필 이미지의 정말, 정말 작은 버전을 반환합니다. 나는 더 큰 이미지를 원한다! 페이스 북의 문서들에 따르면, 그리고 이렇게 많은 것을하는 방법은 다음과 같은 매개 변수를 지정하는 것입니다 : type = large 또는 width = 120 & height = 120하지만 다음과 같은 오류가 발생합니다 :

이와 대답 https://stackoverflow.com/a/7882628/1175289

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

많은 제안, 오히려 "나"요청보다 페이스 북 ID를 사용하는 것이 좋습니다 :

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

(https://developers.facebook.com/docs/graph-api/reference/v2.1/user/picture에) 명시 적으로 상태를 프로필 이미지를 얻기 위해 페이스 북의 문서가 , 그러나 이것은 canonical이 아닌 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/을 사용하고 있기 때문에 API에 누가 me인지 알 수 없으므로 access_token이 필요합니다. 이를 사용자 ID로 바꾸면 (예 :

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

정상적으로 작동합니다.

당신이 URL에 /me/를 계속 단지, 헤이

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

: 너무 답장을 보내 주셔서 감사 예컨대을 사용자의 access_token의 URL을 추가하십시오. 내 어리 석음 때문에 사과하고 싶다. 실제로 app_scoped_user_id를 사용할 때 사용자 ID를 사용하는 것이 작동하지 않는다고 생각 했었습니다. 실제로 이전에 링크를 잘못 작성한 경우였습니다! 그것은 당신이 말한대로 실제로 작동하는 것 같습니다! –

+0

https://graph.facebook.com/v2.1/10204218234286542/picture?type=large에 대한 요청이 브라우저에서 제대로 작동하는 이유는 알 수 없지만 내 앱에서 SLRequest를 통해 요청하면, { "error": { "message": "(# 100) 유형은 small, normal, large, square", "type": "OAuthException", "code": 100}} 중 하나 여야합니다. ?? –

+0

@ george-green SLRequest 매개 변수 사전의 일부로 type = large 매개 변수를 추가해야합니다. 'parameters : @ {@ "type": @ "normal"}'. 그 때 그것은 작동 할 것이다. – fschaper