json Facebook 图 api。从相册中获取照片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9486370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:12:37  来源:igfitidea点击:

Facebook graph api. Get photos from albums

facebookjsonapifacebook-graph-api

提问by Ivan Kozlov

Please help me to make a correct request to Facebook api. Now i've got:

请帮助我向 Facebook api 提出正确的请求。现在我有:

https://graph.facebook.com/me/albums?fields=photos

As the result I get very big Json with a lot of unnecessary info. I tried to do something like this:

结果我得到了非常大的 Json,里面有很多不必要的信息。我试图做这样的事情:

https://graph.facebook.com/me/albums?fields=photos?fields=source,name,id

or like this:

或者像这样:

https://graph.facebook.com/me/albums?fields=photos&fields=source,name,id

But graph api explorer, returned the same big response, or i caught an error. Any ideas how to do more compact response with only necessary info?

但是图形 api explorer返回了相同的大响应,或者我发现了一个错误。任何想法如何只用必要的信息做更紧凑的响应?

采纳答案by Juicy Scripter

You may only use fieldswith properties that exists for objects.

您只能fields与对象存在的属性一起使用。

By issuing GET request to next URL you'll get list of albums ids only:

通过向下一个 URL 发出 GET 请求,您将仅获得专辑 ID 列表:

https://graph.facebook.com/me/albums?fields=id&access_token=...

Same can be achieved by running next FQLquery on albumtable:

同样可以通过FQLalbum表上运行下一个查询来实现:

SELECT aid FROM album WHERE owner=me()

Actually albumsconnection of userdoesn't really contain list of photoobjects but album(which have photosconnection). So to get photos owned by user you'll need iterate over all of his object and getting photos for every album. Again you may use fieldsargument to limit resulting data. This can be done faster if using batch requests.

实际上albumsconnection ofuser并不真正包含photo对象列表,但album(具有photos连接)。因此,要获取用户拥有的照片,您需要遍历他的所有对象并获取每个相册的照片。同样,您可以使用fields参数来限制结果数据。如果使用批处理请求,这可以更快地完成。

Or With FQLit may be done like this (two tables photoand albuminvolved):

或者FQL可以像这样完成(两个表photoalbum涉及):

SELECT pid FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner=me())

回答by Sergio López

Actually there is a better way to achieve this, nesting the request

实际上有更好的方法来实现这一点,嵌套请求

Passing albums as a parameter, and then filter the fields from the connection photos that you want to, for example here I got just everything I need from albums and photos, also this results can be limited

将相册作为参数传递,然后从您想要的连接照片中过滤字段,例如这里我从相册和照片中获得了我需要的一切,而且这个结果可能是有限的

me?fields=albums.fields(id,name,cover_photo,photos.fields(name,picture,source))

回答by Juicy Scripter

Another possible solution is getting album ids first and then iterate over them making this API call:

另一个可能的解决方案是首先获取专辑 ID,然后通过此 API 调用迭代它们:

<ALBUM-ID>/photos?fields=name,source,id

I tested this in graph explorer and it retrieved a reasonable (and readable) json object

我在图形资源管理器中对此进行了测试,它检索了一个合理(且可读)的 json 对象

回答by Max

For photo urls:

对于照片网址:

  1. Ensure that you are an Admin of the Facebook Page.

  2. go to : http://developers.facebook.com/tools/explorer

  3. In the API Navigator, you can see "/me" will bring up the basic information of yourself.

  4. Try typing in "/me/accounts" to see if you can see anything. It should give you an error.

  5. Click on "Get Access Token"

  6. a window will pop-up. Navigate to "Extended Permissions"

  7. Select "manage_pages"

  8. Click "Get Access Token"

  9. Now try "/me/accounts" again. You should see a list of Groups inside the viewing window.

  10. Select the Page you want, and click on the "id" field

  11. Next, on the left window, you can see "Node: " and a + sign. Click on the + sign to see what are the options you have.

  12. Click on the + sign and scroll down to "connections" and select "Albums"

  13. The child-level, select "Photos"

  14. The "Photos" child-level, select "source"

  15. Now click "Submit" on the right hand side. You will see a JSON returned with the url of all the photos in your Facebook Page.

  16. Copy the URL - https://graph.facebook.com/?fields=albums.fields(photos.fields(source)) and plug it into your browser. You should see a JSON of all your photos.

  1. 确保您是 Facebook 主页的管理员。

  2. 去:http: //developers.facebook.com/tools/explorer

  3. 在API Navigator中,可以看到“/me”会调出自己的基本信息。

  4. 尝试输入“/me/accounts”以查看是否可以看到任何内容。它应该给你一个错误。

  5. 点击“获取访问令牌”

  6. 会弹出一个窗口。导航到“扩展权限”

  7. 选择“管理页面”

  8. 点击“获取访问令牌”

  9. 现在再次尝试“/me/accounts”。您应该在查看窗口中看到一个组列表。

  10. 选择您想要的页面,然后单击“id”字段

  11. 接下来,在左侧窗口中,您可以看到“节点:”和一个 + 号。单击 + 号以查看您有哪些选项。

  12. 单击 + 号并向下滚动到“连接”并选择“相册”

  13. 子级,选择“照片”

  14. 在“照片”子级,选择“来源”

  15. 现在点击右侧的“提交”。您将看到一个 JSON 返回,其中包含您 Facebook 页面中所有照片的网址。

  16. 复制 URL - https://graph.facebook.com/?fields=albums.fields(photos.fields(source)) 并将其插入浏览器。您应该会看到所有照片的 JSON。

回答by pashaplus

Each album is a object like user object in facebook, to get photos in that particular album you have to request some thing like below

每个相册都是一个类似于 facebook 中的用户对象的对象,要获取该特定相册中的照片,您必须请求以下内容

http://graph.facebook.com/{ALBUM_ID}?fields=photos&access_token="xxxxx"

回答by Arafin Russell

to get the album list type :

获取专辑列表类型:

me?fields=albums

me?fields=albums

after that type :

在该类型之后:

album_id/photos?fields=source

album_id/photos?fields=source

to get the photos of that particular album

获取该特定相册的照片

回答by Savan Kaneriya

request('GET', '/me/albums?fields=id,name,cover_photo,photos{images{source}},description')

it will show the albums with description and photos with different resolution of that album.
This will need access token

它将显示带有描述的相册和该相册不同分辨率的照片。
这将需要访问令牌

回答by balkaran singh

for Swift 5

对于斯威夫特5

first get albums id like this

首先像这样获取专辑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()




            }
        })
    }

then get albums image with this method

然后使用此方法获取相册图像

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]


            }
        })

    }

album_Id is a number you get from getAlbumsData()

专辑_Id 是您从 getAlbumsData() 获得的数字

回答by balkaran singh

Why is the data unnecessary? Did it return something like this:

为什么数据是不必要的?它是否返回了这样的东西:

{
   "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
      },

?

?

回答by Rohan Dave

GETTING ALBUM_ID

正在获取专辑_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 ]
            }
        })
    }

with above code you will get album_Id and then with that id we can get image like this : GETTING IMAGES FROM ALBUM_ID

使用上面的代码,您将获得专辑 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!)
           }
        }
     })