[英]Facebook graph api. Get photos from albums
請幫助我向 Facebook api 提出正確的請求。 現在我有:
https://graph.facebook.com/me/albums?fields=photos
結果我得到了非常大的 Json,里面有很多不必要的信息。 我試圖做這樣的事情:
https://graph.facebook.com/me/albums?fields=photos?fields=source,name,id
或者像這樣:
https://graph.facebook.com/me/albums?fields=photos&fields=source,name,id
但是圖形 api explorer返回了相同的大響應,或者我發現了一個錯誤。 任何想法如何只用必要的信息做更緊湊的響應?
實際上有更好的方法來實現這一點,嵌套請求
將相冊作為參數傳遞,然后從您想要的連接照片中過濾字段,例如這里我從相冊和照片中獲得了我需要的一切,而且這個結果可能是有限的
me?fields=albums.fields(id,name,cover_photo,photos.fields(name,picture,source))
您只能使用具有對象存在的屬性的fields
。
通過向下一個 URL 發出 GET 請求,您將僅獲得專輯 ID 列表:
https://graph.facebook.com/me/albums?fields=id&access_token=...
SELECT aid FROM album WHERE owner=me()
實際上user
albums
連接並不真正包含photo
對象列表,而是album
(具有photos
連接)。 因此,要獲取用戶擁有的照片,您需要遍歷他的所有對象並獲取每個相冊的照片。 同樣,您可以使用fields
參數來限制結果數據。 如果使用批處理請求,這可以更快地完成。
或者使用FQL
可以這樣做(涉及兩張表photo
和album
):
SELECT pid FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner=me())
另一個可能的解決方案是首先獲取專輯 ID,然后通過此 API 調用迭代它們:
<ALBUM-ID>/photos?fields=name,source,id
我在圖形資源管理器中對此進行了測試,它檢索了一個合理(且可讀)的 json 對象
對於照片網址:
確保您是 Facebook 主頁的管理員。
在API Navigator中,可以看到“/me”會調出自己的基本信息。
嘗試輸入“/me/accounts”以查看是否可以看到任何內容。 它應該給你一個錯誤。
點擊“獲取訪問令牌”
會彈出一個窗口。 導航到“擴展權限”
選擇“管理頁面”
點擊“獲取訪問令牌”
現在再次嘗試“/me/accounts”。 您應該在查看窗口中看到一個組列表。
選擇您想要的頁面,然后單擊“id”字段
接下來,在左側窗口中,您可以看到“節點:”和一個 + 號。 單擊 + 號以查看您有哪些選項。
單擊 + 號並向下滾動到“連接”並選擇“相冊”
子級,選擇“照片”
在“照片”子級,選擇“來源”
現在點擊右側的“提交”。 您將看到一個 JSON 返回,其中包含您 Facebook 頁面中所有照片的網址。
復制 URL - https://graph.facebook.com/ ?fields=albums.fields(photos.fields(source)) 並將其插入瀏覽器。 您應該會看到所有照片的 JSON。
每個相冊都是一個類似於 facebook 中的用戶對象的對象,要獲取該特定相冊中的照片,您必須請求以下內容
http://graph.facebook.com/{ALBUM_ID}?fields=photos&access_token="xxxxx"
獲取專輯列表類型:
me?fields=albums
在該類型之后:
album_id/photos?fields=source
獲取該特定相冊的照片
正在獲取專輯_ID
if((FBSDKAccessToken.current()) != nil)
{
FBSDKGraphRequest(graphPath: "me/albums", parameters: ["fields" : "id"], httpMethod: "GET").start(completionHandler: { (connection, result, error) -> Void in
if (error == nil)
{
let data:[String:AnyObject] = result as! [String : AnyObject]
self.arrdata = data["data"]?.value(forKey: "id") as! [String ]
}
})
}
使用上面的代碼,您將獲得專輯 ID,然后使用該 ID,我們可以獲得這樣的圖像:從 ALBUM_ID 獲取圖像
FBSDKGraphRequest(graphPath: "\(album_Id)/photos", parameters: ["fields": "source"], httpMethod: "GET").start(completionHandler: { (connection, result1, error) -> Void in
if (error == nil)
{
let data1:[String:AnyObject] = result1 as! [String : AnyObject]
let arrdata:[String] = data1["data"]?.value(forKey: "source") as! [String ]
for item in arrdata
{
let url = NSURL(string: item )
let imageData = NSData(contentsOf: url! as URL)
let image = UIImage(data: imageData! as Data)
self.imgArr.append(image!)
}
}
})
request('GET', '/me/albums?fields=id,name,cover_photo,photos{images{source}},description')
它將顯示帶有描述的相冊和該相冊不同分辨率的照片。
這將需要訪問令牌
Swift 5
首先像這樣獲取專輯ID
func getAlbumsData()
{
GraphRequest.init(graphPath: "me", parameters: ["fields":"id,name,albums{name,picture}"]).start(completionHandler: { (connection, userResult, error) in
if error != nil {
print("error occured \(String(describing: error?.localizedDescription))")
}
else if userResult != nil {
print("Login with FB is success")
print()
let fbResult:[String:AnyObject] = userResult as! [String : AnyObject]
self.albumsPhotos = (fbResult["albums"] as! [String:AnyObject])["data"] as? [[String:AnyObject]]
self.tblFbAlbums.reloadData()
}
})
}
然后使用此方法獲取相冊圖像
func fetchalbumsPhotosWithID() {
let graphRequest : GraphRequest = GraphRequest(graphPath: "\(album_Id)/photos", parameters: ["fields": "source"] )
graphRequest.start(completionHandler: { (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print("fetched user: \(result)")
let data = result as! [String:Any]
}
})
}
相冊_Id 是您從 getAlbumsData() 獲得的數字
loginButton.setReadPermissions("public_profile", "email","user_friends","user_photos");
需要這個許可
為什么數據是不必要的? 它是否返回了這樣的東西:
{
"data": [
{
"id": "ID",
"from": {
"name": "Hadrian de Oliveira",
"id": "100000238267321"
},
"name": "Cover Photos",
"link": "https://www.facebook.com/album.php?fbid=FBID&id=ID&aid=AID",
"cover_photo": "ID",
"privacy": "everyone",
"count": 2,
"type": "normal",
"created_time": "2011-10-06T01:31:24+0000",
"updated_time": "2012-02-22T17:29:50+0000",
"can_upload": false
},
?
for android
new GraphRequest(
facebookToken,
String.format("/%s/photos", idAlbum),
parameters,
HttpMethod.GET,
response -> {
try {
JSONArray photoArray = response.getJSONObject().getJSONArray("data");
photosAlbumAfterPagination = response.getJSONObject().getJSONObject("paging").getJSONObject("cursors").getString("after");
Gson gson = new Gson();
Type type = new TypeToken<List<FacebookPhotoResponse>>() {
}.getType();
List<FacebookPhotoResponse> list = gson.fromJson(photoArray.toString(), type);
} catch (JSONException e) {
e.printStackTrace();
}
}
).executeAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.