权限错误 - 尝试使用Android facebook sdk获取朋友

时间:2011-06-11 15:25:56

标签: java android facebook facebook-graph-api

我正在尝试为我的Android应用添加一项功能,允许用户“签入”其他标记为签到的人。 我有checkins方法没有问题,可以通过添加用户ID作为参数来标记一些(参见下面的代码)

public void postLocationTagged(String msg, String tags, String placeID, Double lat, Double lon) {
    Log.d("Tests", "Testing graph API location post");
    String access_token = sharedPrefs.getString("access_token", "x");
     try {
         if (isSession()) {
            String response = mFacebook.request("me");
            Bundle parameters = new Bundle();
            parameters.putString("access_token", access_token);
            parameters.putString("place", placeID);
            parameters.putString("Message",msg);
            JSONObject coordinates = new JSONObject();
            coordinates.put("latitude", lat);
            coordinates.put("longitude", lon);
            parameters.putString("coordinates",coordinates.toString());
            parameters.putString("tags", tags);
            response = mFacebook.request("me/checkins", parameters, "POST");
            Toast display = Toast.makeText(this, "Checkin has been posted to Facebook.", Toast.LENGTH_SHORT);
            display.show();
            Log.d("Tests", "got response: " + response);
            if (response == null || response.equals("") || 
                    response.equals("false")) {
               Log.v("Error", "Blank response");
            }
        } else {
         // no logged in, so relogin
         Log.d(TAG, "sessionNOTValid, relogin");
         mFacebook.authorize(this, PERMS, new LoginDialogListener());
        }
     } catch(Exception e) {
         e.printStackTrace();
     }
}

这很好用(我发布它是为了帮助其他人!),我遇到的问题是我正在尝试创建一个用户朋友列表,以便他们可以选择他们想要的朋友标签。我有方法getFriends(见下文),然后我将用它来生成一个AlertDialog,用户可以从中选择,从而为我提供在上面的“postLocationTagged”方法中使用的id。

public void getFriends(CharSequence[] charFriendsNames,CharSequence[] charFriendsID, ProgressBar progbar) {
    pb = progbar;
    try {
        if (isSession()) {
            String access_token = sharedPrefs.getString("access_token", "x");
            friends = charFriendsNames;
            friendsID = charFriendsID;
           Log.d(TAG, "Getting Friends!");
           String response = mFacebook.request("me");
           Bundle parameters = new Bundle();
           parameters.putString("access_token", access_token);
           response = mFacebook.request("me/friends", parameters, "POST");

           Log.d("Tests", "got response: " + response);
           if (response == null || response.equals("") || 
                   response.equals("false")) {
              Log.v("Error", "Blank response");
           }

        } else {
        // no logged in, so relogin
        Log.d(TAG, "sessionNOTValid, relogin");
        mFacebook.authorize(this, PERMS, new LoginDialogListener());
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}

当我查看日志中的响应时,它会显示:

“得到回应:{”错误“:{”type“:”OAuthException“,”message“:”(#200)权限错误“}}”

我查看了graphAPI文档并搜索了类似的问题,但无济于事!我不确定我是否需要为应用程序请求额外的权限,或者这是你不允许做的事情!任何帮助/建议将不胜感激。

3 个答案:

答案 0 :(得分:2)

您可能需要以下权限:

  • user_checkins
  • friends_checkins
  • read_friendlists
  • manage_friendlists
  • publish_checkins

检查API文档中的相关内容。在此之前,请确保哪一行导致此权限错误并尝试修复它。

答案 1 :(得分:2)

解决方案是在向Facebook图形API发出请求时实现RequestListener。我有新的getFriends()方法(见下文),它使用AsyncGacebookRunner来请求数据。

public void getFriends(CharSequence[] charFriendsNames,String[] sFriendsID, ProgressBar progbar) {
    try{
        //Pass arrays to store data
        friends = charFriendsNames;
        friendsID = sFriendsID;
        pb = progbar;
        Log.d(TAG, "Getting Friends!");
        //Create Request with Friends Request Listener
        mAsyncRunner.request("me/friends", new FriendsRequestListener());
    } catch (Exception e) {
        Log.d(TAG, "Exception: " + e.getMessage());
    }
}

AsyncFacebookRunner使用实现RequestListener类的自定义FriendsRequestListener(见下文)发出请求;

private class FriendsRequestListener implements RequestListener {
    String friendData;

    //Method runs when request is complete
    public void onComplete(String response, Object state) {
        Log.d(TAG, "FriendListRequestONComplete");
        //Create a copy of the response so i can be read in the run() method.
        friendData = response; 

        //Create method to run on UI thread
        FBConnectActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                try {
                    //Parse JSON Data
                    JSONObject json;
                    json = Util.parseJson(friendData);

                    //Get the JSONArry from our response JSONObject
                    JSONArray friendArray = json.getJSONArray("data");

                    //Loop through our JSONArray
                    int friendCount = 0;
                    String fId, fNm;
                    JSONObject friend;
                    for (int i = 0;i<friendArray.length();i++){
                        //Get a JSONObject from the JSONArray
                        friend = friendArray.getJSONObject(i);
                        //Extract the strings from the JSONObject
                        fId = friend.getString("id");
                        fNm = friend.getString("name");
                        //Set the values to our arrays
                        friendsID[friendCount] = fId;
                        friends[friendCount] = fNm;
                        friendCount ++;
                        Log.d("TEST", "Friend Added: " + fNm);
                    }

                    //Remove Progress Bar
                    pb.setVisibility(ProgressBar.GONE);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FacebookError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

随意在您自己的项目中使用任何此类代码,或者询问有关它的任何问题。

答案 2 :(得分:0)

你可以private static final String[] PERMISSIONS = new String[] {"publish_stream","status_update",xxxx}; xxx是预先提交