简体   繁体   English

无需登录即可从Facebook图形API检索照片

[英]Retrieve photos from facebook graph api without login

I have a php app where my registered users login to facebook. 我有一个PHP应用程序,我的注册用户登录到Facebook。 I use $facebook->getLogoutUrl(); 我使用$facebook->getLogoutUrl(); . Then I retieve all their albums and display their id and names in an HTML select tag. 然后,我将所有的相册重新整理,并在HTML select标签中显示其ID和名称。 The user select one and the id of the album is stored in the DB. 用户选择一个,专辑的ID存储在DB中。

Here everything works great. 在这里一切都很好。

Then an anonymous user visit my website and I want him to see my registered user photos (public photos of the albums selected by the registered users) (they are not logged in my site, they are not logged in facebook). 然后,一个匿名用户访问我的网站,我希望他看到我的注册用户照片(注册用户选择的相册的公开照片)(他们没有登录我的网站,也没有登录Facebook)。 At the moment it only works if the anonymous user is logged in facebook. 目前,仅当匿名用户登录Facebook时,它才有效。 But I want to diplay photos even if the anon is not logged in facebook. 但是,即使匿名用户未登录Facebook,我也想显示照片。

$photos = $facebook->api('/'.$idAlbum.'?fields=id,name,link,photos.fields(id,picture,source)');

I have tried a lot for 2 weeks but nothing seems to work. 我已经尝试了2周,但似乎没有任何效果。 I have tried without login, I have tried with an app access token, I have tried: 我尝试不登录,尝试使用应用访问令牌,尝试过:

https://graph.facebook.com/oauth/access_token?client_id=XXXXXX&client_secret=YYYYYY&grant_type=client_credentials

Can anyone help me? 谁能帮我? or tell me if is not possible to my app to retireve photos from an album. 或告诉我我的应用程序是否无法从相册中删除照片。 I searched a way to login my app but I only find app access token ( https://developers.facebook.com/docs/howtos/login/login-as-app/ ) but as it say it can make requests as the app, not as a user 我搜索了一种登录应用程序的方法,但仅找到应用程序访问令牌( https://developers.facebook.com/docs/howtos/login/login-as-app/ ),但它说它it can make requests as the app, not as a user

In order to access a users information, you're going to need a valid access token for that user. 为了访问用户信息,您将需要该用户的有效访问令牌。 An application access token can't be used to do anything on behalf of a user. 应用程序访问令牌不能用于代表用户执行任何操作。

Access tokens expire after a while and that prevents applications from searching though the users information when the user is not actually using the application. 访问令牌会在一段时间后过期,这会阻止应用程序在用户未实际使用应用程序时搜索用户信息。

If you want to go all the way - just download all the photo's from the album and store them on your server... If you can't do that, then you'll have to look into extending the album owner's access token so that you can use it when querying their album. 如果您想一路走-只需下载相册中的所有照片并将它们存储在服务器上...如果您不能这样做,则必须考虑扩展相册所有者的访问令牌,以便您可以在查询他们的相册时使用它。 As far as the application is considered, with that extended access token, it is in actual fact the album owner who is making the request. 就应用程序而言,使用该扩展访问令牌,实际上是发出请求的是相册所有者。

Taken from the Facebook docs : 取自Facebook文档

Extending client-side user access tokens 扩展客户端用户访问令牌

When a person completes a client-side auth flow and you retrieve their user access token, by default the token is only valid for one to two hours. 当人员完成客户端身份验证流程并且您检索其用户访问令牌时,默认情况下,该令牌仅有效一到两个小时。

You can exchange this token for a longer-lived one that's valid for up to 60 days by passing it to the /oauth endpoint from your server with a grant_type parameter of fb_exchange_token. 您可以通过使用fb_exchange_token的grant_type参数将令牌从服务器传递到/ oauth端点,从而将该令牌交换为有效期最长为60天的令牌。 [Note that it must be sent from the server so that the App Secret is not exposed.] Here's what that looks like: [请注意,必须从服务器发送它,这样才不会暴露App Secret。]如下所示:

 https://graph.facebook.com/oauth/access_token? https://graph.facebook.com/oauth/access_token?  \n    grant_type=fb_exchange_token& grant_type = fb_exchange_token&           \n    client_id=APP_ID& CLIENT_ID = APP_ID&\n    client_secret=APP_SECRET& client_secret = APP_SECRET&\n    fb_exchange_token=SHORT_LIVED_ACCESS_TOKEN fb_exchange_token = SHORT_LIVED_ACCESS_TOKEN 

The response from this endpoint will include the long-lived access token. 来自此端点的响应将包含长期访问令牌。 This will be a different string to the original token, so if you're storing these tokens, replace the old one. 这将是与原始令牌不同的字符串,因此,如果要存储这些令牌,请替换旧令牌。

Basically you'll have to do this as soon as any (registered) user performs a succesful login and store the token in your database. 基本上,一旦任何(注册)用户成功登录并将令牌存储在数据库中,您就必须这样做。

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

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