facebook: permanent Page Access Token?

后端 未结 16 2370
情书的邮戳
情书的邮戳 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: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.

提交回复
热议问题