NativeScript / nativescript-facebook

NativeScript plugin, wrapper of native Facebook SDK for Android and iOS
Apache License 2.0
79 stars 50 forks source link

How to use `requestPublishPermissions` to add scopes #75

Closed imikemiller closed 6 years ago

imikemiller commented 6 years ago

I am looking for some guidance on how to add additional scopes to the login request using the SDK method requestPublishPermissions. It is not clear how this can be achieved without changing the values hardcoded at login-manager.android.js:5 and login-manager.ios.js:5.

DimitarTachev commented 6 years ago

Hi @imikemiller, the read permissions of the login method cannot be overridden. The proper way of requesting additional permissions is using the requestReadPermissions or requestPublishPermissions methods depending on the type of the permissions.

Bear in mind that this could also require some additional actions in your facebook app (the one in developers.facebook.com). I suggest you take a look at issue #60 describing some of the possible difficulties when requesting additional permissions.

imikemiller commented 6 years ago

@DimitarTachev thanks very much. I am clear on the steps in facebook I am just unclear on how to initialise the login button with the additional requested scopes.

If I do something like this

let nsFacebook = require('nativescript-facebook');
application.on(application.launchEvent, function (args) {
  nsFacebook.init("XXXXXXXXXXXXXXX");
  nsFacebook.requestPublishPermissions(["public_profile", "email","publish_actions"]);
});

The login function called by the button still uses the hardcoded LOGIN_PERMISSIONS. Where in the flow can i call these requestReadPermissions or requestPublishPermissions functions?

Thanks again, any input always greatly appreciated

DimitarTachev commented 6 years ago

Hi @imikemiller,

The login button exposed from the Facebook SDK is meant to be used for simple logins. If you try to request both read and publish permissions in nested callbacks you will get the following error from Facebook: "You are requesting permissions inside the completion block of an existing login.This is unsupported behavior. You should request additional permissions only when they are needed, such as requesting for publish_actions when the user performs a sharing action."

When you need additional permissions, you could request them using requestReadPermissions or requestPublishPermissions with the following code:

const readPermissions = ["public_profile", "email", "user_likes", "user_friends", "user_location", "pages_show_list"];
Facebook.requestReadPermissions(readPermissions, (error, data) => {
... use data.token
...

or

const publishPermissions = ["publish_actions", "manage_pages", "publish_pages"];
Facebook.requestPublishPermissions(publishPermissions, (error, data) => {
... use data.token
...

I've just fixed an issue with the callback methods in pull request #76. Bear in mind that your request permissions callbacks could be called multiple times on Android before we release this fix.

In other words, call the functions just before you need them in your app as that's the Facebook recommended flow.

tbozhikov commented 6 years ago

I'm closing this, write again if needed.