Featured image of post Open a Facebook Post Directly in Instagram Stories.

Open a Facebook Post Directly in Instagram Stories.

📌 Introduction

When users share a Facebook post through a link sticker in an Instagram story, clicking the link sticker will by default open the post in Instagram’s in-app browser, prompting a login. To allow users to open the post directly in the Facebook app, you can refer to the following content.

You can use the Python script below to simulate a mobile device sending a request to Facebook to obtain the graphqlid of the shared post. With the graphqlid, the share post can be directly opened in the Facebook app on both iOS and Android, rather than in a in-app browser.

當使用者在 Instagram 的限時動態透過連結的貼紙分享 Facebook 的文章,其他使用者直接點擊連結時,預設會在 Instagram 的內建瀏覽器開啟文章,這樣會出現登入的提示,為了讓使用者可以直接在 Facebook 的應用程式內開啟文章,可以參考以下的內容。

可以使用下方的 Python 程式來模擬行動裝置對 Facebook 發送請求,來取得分享文章的 graphqlid,有了 graphqlid 可以直接開啟 iOS 與 Android 上的 Facebook 應用程式中開啟文章,而不是在瀏覽器中。

💻 Code

import re
import requests

# fetch url and follow redirect
url = input("The sharing URL of a Facebook post: ")

# setting User Agent
headers = {
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1'
}

response = requests.get(url, headers=headers)
text = str(response.text).replace(f'\\u00253D', '%3D')

# use regex to find "fb:\/\/story\/?graphqlid= ... &wtsid= ..."
graphqlid_match = re.search(r"graphqlid=([^&]+)", text)
wtsid_match = re.search(r"wtsid=([^&\"]+)", text)
if graphqlid_match and wtsid_match:
    if url.split('/')[3] == 'share' and url.split('/')[4] == 'p':  # ex: https://www.facebook.com/share/p/ZdUX3zypra1Pa1HK/
        print(f"https://1one2two.github.io/open_facebook/?share={url.split('/')[5]}&graphqlid={graphqlid_match.group(1)}")
    elif url.split('/')[3] == 'share' and url.split('/')[4] != 'p':  # ex: https://www.facebook.com/share/ZdUX3zypra1Pa1HK/
        print(f"https://1one2two.github.io/open_facebook/?share={url.split('/')[4]}&graphqlid={graphqlid_match.group(1)}")
    elif url.split('/')[4] == 'posts':  # ex: https://www.facebook.com/ec.wife/posts/pfbid02pKNKt511kmuek8fYsKp6C5BVBn894mFTBfz8TLRukAEschy31HUZgGfR8LkpFwDgl
        print(f"https://1one2two.github.io/open_facebook/?postsname={url.split('/')[3]}&postsid={url.split('/')[5]}&graphqlid={graphqlid_match.group(1)}")
else:
    print("Some error occurred. Please try again.")

🗒️ Notes

Use Safari open website on iOS: window.location.href= "x-safari-https://example.com/"

Use Google chrome open website on Android: window.location.href = "intent://example.com/#Intent;scheme=https;package=com.android.chrome;end"

Use URL scheme open Facebook post on Android: window.location.href = "fb://facewebmodal/f?href=https://www.facebook.com/share/XXXXXXXXXXXXXXXXXXX/"

Open Facebook post on iOS, can use the above Python script to get the graphqlid: window.location.href = "fb://story/?graphqlid=XXXXXXXXXX"