簡體   English   中英

權限錯誤 - 嘗試使用 android facebook sdk 結交朋友

[英]Permissions Error - Trying to get friends using android facebook sdk

我正在嘗試向我的 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();
     }
}

這很好用(我已經發布它以防它對其他人有幫助)。 我遇到的問題是我正在嘗試創建用戶朋友的列表,以便他們可以 select 他們想要標記的朋友。 我有方法 getFriends(見下文),然后我將使用它來生成一個 AlertDialog,用戶可以 select 從中給我在上述“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();
    }
}

當我查看日志中的響應時,它顯示:

"得到響應:{"error":{"type":"OAuthException", "message":"(#200) 權限錯誤"}}"

我查看了 graphAPI 文檔並搜索了類似的問題,但無濟於事。 我不確定我是否需要為應用程序請求額外的權限,或者這是您不允許做的事情! 任何幫助/建議將不勝感激。

您可能需要以下權限:

  • user_checkins
  • 朋友簽到
  • 閱讀好友列表
  • 管理好友列表
  • 發布簽入

檢查 API 文檔中的相關內容。 在此之前,請確保哪一行導致此權限錯誤並嘗試修復它。

解決方案是在向 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 class 的自定義 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();
                }
            }
        });
    }

隨意在您自己的項目中使用任何此代碼,或詢問有關它的任何問題。

您可以private static final String[] PERMISSIONS = new String[] {"publish_stream","status_update",xxxx}; xxx 是前提

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM