phonegap cordova facebook插件登录失败时出现JSON错误

时间:2014-08-09 00:47:39

标签: android facebook angularjs cordova ionic-framework

我只是想测试一下这个plugin是否正常工作,我很难做到这一点。现在花费了大约48小时,肯定需要帮助。

我的设置非常简单。我正在使用ionic / angularjs。对于那些不熟悉这些的人,我不相信它是相关的,但我包含这个以便你知道我的设置(以及$scope变量的奇怪语法...)

基本上,您在下面看到的是一种登录方法,在调用时,执行facebookConnectPlugin.login。我订购了参数(permissions,successCallback,failCallback),我认为这是正确的顺序,而不是文档中显示的顺序。

我的successCallback函数只是发出成功登录的警报并发出accessToken。我的failCallback函数只是发出登录失败的警报并给出响应。

当我在我的Android设备上运行代码并单击登录时,它无法登录,响应变量只是一个表示JSON错误的字符串。没有其他信息。我完全迷失了,希望有人能告诉我这是什么问题。

我一步一步地遵循了android指南,没有任何错误,所以我认为我应该可以使用该插件。任何人都知道为什么它不工作???我能做些什么来进一步解决这个问题?

function loginctrl($scope){

$scope.login = function(){

facebookConnectPlugin.login('public_profile,email,user_friends',
  function(response){
  alert("success!"+response.status);
  if (response.status=="connected"){
    console.log(response.authResponse.userID+" "+response.authResponse.accessToken);
  }
},function(response){
  console.error("I'm an error" + response);
  alert("failed!"+response);
});
 }
}

编辑:看起来错误JSON Error来自未能对插件执行login操作。具体做法是:

login: function (permissions, s, f) {
        cordova.exec(s, f, "FacebookConnectPlugin", "login", permissions);
    },

cordova.exec函数执行以下操作。注意第一行throws JSONException。当插件抛出JSONException时,它会发回一个错误,指出JSON错误。因此,下面的插件在某处有错误......仍在调查以确定它是什么......

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    if (action.equals("login")) {
        Log.d(TAG, "login FB");
        // Get the permissions
        String[] arrayPermissions = new String[args.length()];
        for (int i = 0; i < args.length(); i++) {
            arrayPermissions[i] = args.getString(i);
        }

        List<String> permissions = null;
        if (arrayPermissions.length > 0) {
            permissions = Arrays.asList(arrayPermissions);
        }

        // Get the currently active session
        Session session = Session.getActiveSession();

        // Set a pending callback to cordova
        loginContext = callbackContext;
        PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
        pr.setKeepCallback(true);
        loginContext.sendPluginResult(pr);

        // Check if the active session is open
        if (session != null && session.isOpened()) {
            // Reauthorize flow
            boolean publishPermissions = false;
            boolean readPermissions = false;
            // Figure out if this will be a read or publish reauthorize
            if (permissions == null) {
                // No permissions, read
                readPermissions = true;
            }
            // Loop through the permissions to see what
            // is being requested
            for (String permission : arrayPermissions) {
                if (isPublishPermission(permission)) {
                    publishPermissions = true;
                } else {
                    readPermissions = true;
                }
                // Break if we have a mixed bag, as this is an error
                if (publishPermissions && readPermissions) {
                    break;
                }
            }

            if (publishPermissions && readPermissions) {
                callbackContext.error("Cannot ask for both read and publish permissions.");
            } else {
                // Set up the new permissions request
                Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(cordova.getActivity(), permissions);
                // Set up the activity result callback to this class
                cordova.setActivityResultCallback(this);
                // Check for write permissions, the default is read (empty)
                if (publishPermissions) {
                    // Request new publish permissions
                    session.requestNewPublishPermissions(newPermissionsRequest);
                } else {
                    // Request new read permissions
                    session.requestNewReadPermissions(newPermissionsRequest);
                }
            }
        } else {
            // Initial login, build a new session open request.

            // - Create a new session and set the application ID
            session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
            Session.setActiveSession(session);
            // - Create the request
            Session.OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());
            // - Set the permissions
            openRequest.setPermissions(permissions);
            // - Set the status change call back
            openRequest.setCallback(new Session.StatusCallback() {
                @Override
                public void call(Session session, SessionState state, Exception exception) {
                    onSessionStateChange(state, exception);
                }
            });

            // Can only ask for read permissions initially
            session.openForRead(openRequest);
        }
        return true;

 // Other statements below to handle when the execute is not 'login'

1 个答案:

答案 0 :(得分:3)

我是个菜鸟。

我的问题是我的权限是'public_profile,email,user_friends'之类的字符串。正确的类型是像['public_profile','email','user_friends']这样的数组。