Dynamic Image for Facebook Opengraph og:image meta tag

北慕城南 提交于 2020-01-01 06:08:06

问题


I am trying to share an image from my website on facebook. The image can be dynamic, but other meta will remain the same.

Is there a way I can have dynamic data in OG:IMAGE tag, or will I have to go with the other option of FB Post APIs.


回答1:


Yes and no. Facebook scrapes your site once, and cached the metadata that it finds, unless you specifically go here and force the scraper to crawl your site again. The cache does generally expire (perhaps after 1-2 days?), so when requested again outside of this cache period, Facebook will crawl the site again.

You can have a dynamically generated og:image meta tag, but it will only be read the one time (per cache period), and only that instance of the image will be saved.

For example, if user A shares your page, and your page returns imageA.png in the og:image tag, then that is the image that will be associated with your page's metadata.

If user B then shares the same page within the same cache period, Facebook will forgo the metadata scraping and assume that imageA.png is still a valid og:image.




回答2:


Take a look at this Gist.

You should ping facebook to recrawl your page again every time you update your og:image tag.

def share_facebook_fanpage(link,msg,PAGE_ID,OAUTH_ACCESS_TOKEN,apiversion='v2.8'):
    BASE_URL = "https://graph.facebook.com/%s" % apiversion
    POST_URL = "%s/%s/feed" % (BASE_URL, PAGE_ID)

    # force facebook scape the link first to avoid not showing thumb
    f = requests.post(BASE_URL, data={
        'id': link,
        'scrape': True,
        'access_token': OAUTH_ACCESS_TOKEN
    })

    # share the link
    r = requests.post(POST_URL, data={'access_token': OAUTH_ACCESS_TOKEN, 'link': link, 'message':msg})


来源:https://stackoverflow.com/questions/15832055/dynamic-image-for-facebook-opengraph-ogimage-meta-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!