facebook: permanent Page Access Token?

后端 未结 16 2093
情书的邮戳
情书的邮戳 2020-11-22 02:39

I work on a project that has facebook pages as one of its data sources. It imports some data from it periodically with no GUI involved. Then we use a web app to show the dat

相关标签:
16条回答
  • 2020-11-22 03:14

    I tried these steps: https://developers.facebook.com/docs/marketing-api/access#graph-api-explorer

    Get Permanent Page Access Token

    • Go to Graph API Explorer
    • Select your app in Application
    • Paste the long-lived access token into Access Token
    • Next to Access Token, choose the page you want an access token for. The access token appears as a new string.
    • Click i to see the properties of this access token
    • Click “Open in Access Token Tool” button again to open the “Access Token Debugger” tool to check the properties

    One Tip, it only worked for me when the page language is english.

    0 讨论(0)
  • 2020-11-22 03:15

    Thanks to @donut I managed to get the never expiring access token in JavaScript.

    // Initialize exchange
    fetch('https://graph.facebook.com/v3.2/oauth/access_token?grant_type=fb_exchange_token&client_id={client_id}&client_secret={client_secret}&fb_exchange_token={short_lived_token}')
    .then((data) => {
        return data.json();
    })
    .then((json) => {
        // Get the user data
        fetch(`https://graph.facebook.com/v3.2/me?access_token=${json.access_token}`)
        .then((data) => {
            return data.json();
        })
        .then((userData) => {
            // Get the page token
            fetch(`https://graph.facebook.com/v3.2/${userData.id}/accounts?access_token=${json.access_token}`)
            .then((data) => {
                return data.json();
            })
            .then((pageToken) => {
                // Save the access token somewhere
                // You'll need it at later point
            })
            .catch((err) => console.error(err))
        })
        .catch((err) => console.error(err))
    })
    .catch((err) => {
        console.error(err);
    })
    

    and then I used the saved access token like this

    fetch('https://graph.facebook.com/v3.2/{page_id}?fields=fan_count&access_token={token_from_the_data_array}')
    .then((data) => {
        return data.json();
    })
    .then((json) => {
        // Do stuff
    })
    .catch((err) => console.error(err))
    

    I hope that someone can trim this code because it's kinda messy but it was the only way I could think of.

    0 讨论(0)
  • 2020-11-22 03:18

    If you have facebook's app, then you can try with app-id & app-secret.

    Like :

    access_token={your-app_id}|{your-app_secret}
    

    it will don't require to change the token frequently.

    0 讨论(0)
  • 2020-11-22 03:19

    While getting the permanent access token I followed above 5 steps as Donut mentioned. However in the 5th step while generating permanent access token its returning the long lived access token(Which is valid for 2 months) not permanent access token(which never expires). what I noticed is the current version of Graph API is V2.5. If you trying to get the permanent access token with V2.5 its giving long lived access token.Try to make API call with V2.2(if you are not able to change version in the graph api explorer,hit the API call https://graph.facebook.com/v2.2/{account_id}/accounts?access_token={long_lived_access_token} in the new tab with V2.2) then you will get the permanent access token(Which never expires)

    0 讨论(0)
提交回复
热议问题