简体   繁体   English

Facebook Graph API-访问用户的照片时如何避免请求限制?

[英]Facebook Graph API - How to avoid request limits when accessing a user's photos?

I need to fetch user photos and albums using the Facebook Graph API. 我需要使用Facebook Graph API来获取用户照片和相册。

As far as I understand (while reading the docs) this is a two step procedure, 1. fetching the id for each photo from the photos edge, 2. using the returned array of ids, fetching individual photo nodes. 据我了解(在阅读文档时),这是一个两步过程,1.从photos边缘获取每张照片的ID,2.使用返回的ID数组,获取单个照片节点。

Fetching individual photo nodes could mean hundreds of requests/s, while having a lot of photos on my profile I have hit the app request limits after a few minutes of testing. 提取单个照片节点可能意味着每秒数百个请求,而我的个人资料上有很多照片,经过几分钟的测试,我达到了应用程序请求限制。

Is there maybe a way to fetch all photos at once (or in fewer requests), in order to avoid request limits? 为了避免请求限制,也许有一种方法可以一次(或以更少的请求)获取所有照片?

example implementation: 示例实现:

  ...

  var photos = [];

  function fetchPhotos(){
    fetch('https://graph.facebook.com/v3.1/me/photos?type=uploaded&access_token=' + accesstoken, {
      method: 'GET'
    })
    .then(photos => photos.json())
    .catch(error => console.error('Error:', error))
    .then(photos => {

      this.fetchPhoto(photos);

    });
  }

  function fetchPhoto(photos){
    for(var i=0; i < photos.data.length; i++){
      fetch('https://graph.facebook.com/v3.1/' + photos.data[i].id + '?fields=link,width,height&access_token=' + accesstoken, {
        method: 'GET'
      })
      .then(photo => photo.json())
      .catch(error => console.error('Error:', error))
      .then(photo => {

        photos.push(photo);

      });
    }
  }

  ...

Thanks! 谢谢!

You can already get a list with all the fields with one call: 您只需一次调用就可以获取所有字段的列表:

https://graph.facebook.com/v3.1/me/photos?fields=link,width,height&access_token=...

Side Note: If that is client code, why not use the official JS SDK? 旁注:如果这是客户端代码,为什么不使用官方的JS SDK? That way, you don´t need to deal with the Access Token, it gets added automatically. 这样,您无需处理访问令牌,它会自动添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM