facebook / facebook-nodejs-business-sdk

Node.js SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
490 stars 226 forks source link

No way to override access_token #191

Open joshwreford opened 3 years ago

joshwreford commented 3 years ago

Because the access_token is set when you first init the api e.g. const api = adsSdk.FacebookAdsApi.init(accessToken); it means that you are unable to switch it if required.

For example, I'm currently using a marketing access_token to get my parent business and to then get all facebook pages associated with that parent business manager. I can do this by running the method... parentBusinessManager.getClientPages();

I would then like to get all the instagram accounts associated with each of those Facebook pages. But if I try to do the following: page.getInstagramAccounts(['username']) It will throw an error saying that I need to use a "Page access token".

This is frustrating because the other access_token works for everything else. I actually get the page access_token back when I request the client pages, but there's no way to use it with the SDK at that point.

For example, I was hoping I could just override the access_tokenby passing it into the params like so:

this.page.getInstagramAccounts(
      ['username'],
      { access_token: accessToken },
    );

But, if you look at the code below in api.js, the access_token is overwritten on the params before the params object is used. https://github.com/facebook/facebook-nodejs-business-sdk/blob/4d52015eb512d9900bda7a93933e1b121b48915f/src/api.js#L77

It would be nice if the call() function checked whether you've passed a new access_token in the params argument, and if so, used it. Otherwise, it could fallback to using this.access_token. This seems like it would be a simple, yet powerful change that would allow a lot more flexibility with the SDK.

deanifrog commented 3 years ago

I agree, if no access_token is present in a requests params, then the init access token should be a fallback.

In the case of App Access Tokens, you are able to pass off the token creation to the graph api like so: curl -i -X GET "https://graph.facebook.com/{api-endpoint}&access_token={your-app_id}|{your-app_secret}"

adxy commented 2 years ago

@joshwreford I would love to pick this up, can you assign this to me?

joshwreford commented 2 years ago

@joshwreford I would love to pick this up, can you assign this to me?

I don't have permissions to assign it I'm afraid

brantian commented 2 years ago

Any updates on this? I'm having a similar problem. I'm working with multiple access tokens from multiple users and I am having problem with concurrent operations

alokpaidalwar commented 2 years ago

Hi, any update on this ?

michaelgainyo commented 2 years ago

@joshwreford I'm fairly new this api but have you tried something like this?

const old = api.accessToken
api.accessToken = 'your-page-token'
// make call to get ig pages
// then reset
api.accessToken = old