简体   繁体   中英

Error: "You must provide an app access token, or a user access token that is an owner or developer of the app" using Facebook Login SDK

I have the problem that users can use our system to request reports of their ads on facebook.

These are the functions I use in the frontend (React):

export function initFacebookSdk() {
  return new Promise(resolve => {
    // wait for facebook sdk to initialize before starting the react app
    window.fbAsyncInit = function () {
      window.FB.init({
        appId: 'app-id',
        cookie: true,
        xfbml: true,
        version: 'v14.0'
      });
      resolve()
    };   
  });
}

export const FbLogin = () => {
  loadFacebookSDK(document, "script", "facebook-jssdk")
  initFacebookSdk()
  return new Promise( resolve => {
    window.FB.login(response => {
      if (response.authResponse) {
        resolve(response);
      } else {
        resolve(response);
      }
    }, {scope: 'ads_management,ads_read'});
  })
}

And so I call the function

const response = await FbLogin();

I send the token to the backend and this is how it uses the route:

try {
  const { data: accountsInfo } = await axios({
    url: `https://graph.facebook.com/v${FB_GRAPH_API_VERSION}/me/adaccounts?access_token=${accessToken}&pretty=1&limit=100`,
    method: 'GET',
  });
  

  res.status(200).json({success: true, accountsInfo})
  
} catch (err) {
  console.log(err);

  res.status(500).json({success: false})
}

And this is the error that I get when trying to use the user token:

'OAuth "Facebook Platform" "invalid_request" "(#100) You must provide an app access token, or a user access token that is an owner or developer of the app"'

Does anyone know why this is happening to me? help :(

you are not showing how you are sending the token to backend, and do you really receive it ?? your question need more details , and about your way of using the Facebook sdk, as long as you are using react way you don't use the power of the state and custom hooks to reduce the possibility of the error, like this :

 export const useFacebookSdk = () => { const [fcbSdk, setFcbSdk] = useState(); useEffect(() => { window.fbAsyncInit = function () { FB.init({ appId: 'app-id', autoLogAppEvents: true, cookie: true, xfbml: true, version: 'v14.0', status: true, }); setFcbSdk((prev) => ({ ...prev, login: () => ((scope) => FB.login((r, handleLoginRes) => handleLoginRes(r), { scope })), logout: () => (() => { FB.logout() }), FcbApiReq: ((path, method, params, cb) => FB.api(path, method, params, cb)), getLoginStatus: (() => { let status; FB.getLoginStatus(e => { return status = e }) return status }), })) } }, []) return { ...fcbSdk } } // use it like this // import { login, logout, FcbApiReq, getLoginStatus} from "./pathOfFile.js" // or if it s in the same file // { login, logout, FcbApiReq, getLoginStatus} =useFacebookSdk()

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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