其他用户Facebook Feed错误

时间:2017-03-29 06:28:43

标签: java android facebook facebook-graph-api codenameone

我正在尝试使用图形api在Codename中获取Facebook Feed,但它仅适用于我自己的帐户,每当其他用户尝试使用我的应用程序获取其Feed时,就会抛出错误。

以下是我的代码

public class Test {

    private Form current;
    private Resources theme;
    Form facebook;
    Toolbar tb;
    Image user;
    Label userLabel;
    Label username;
    Login loginfb;
    String clientId = "158093724691158";
    String redirectURI = "http://www.codenameone.com/";
    String clientSecret = "4f5b275ae702f7b6fde9bc50bfe3b5e3";
    Label proLabel;
    Label fname;
    Label fmail;
    Label fgender;
    Container container12;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
    }

    public void start() {
        if(current != null){
            current.show();
            return;
        }
        try{
        Splash spl = new Splash();
        spl.show();

        new java.util.Timer().schedule(
                new java.util.TimerTask() {
            @Override
            public void run() {
                facebook.show();
            }
        } , 4000);
        }catch(IOException e){
            e.printStackTrace();
        }
       facebook = new Form("Facebook", new BoxLayout(BoxLayout.Y_AXIS));
        tb = new Toolbar();
        facebook.setToolbar(tb);

       user = theme.getImage("user.png");
        userLabel = new Label(user);
        username = new Label("Visitor");
        Button facebooklogin = new Button("Login with Facebook");
        Button linked = new Button("Login with LinkedIn");
        //linked.setUIID("linkedButton");

        linked.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {

                          Oauth2 auth2 = new Oauth2("https://www.linkedin.com/oauth/v2/authorization?response_type=code", 
                "81lq3qpacjvkcu", 
                "https://www.codenameone.com","r_fullprofile%20r_emailaddress","https://www.linkedin.com/uas/oauth2/accessToken","vzwWfamZ3IUQsJQL");
        Oauth2.setBackToParent(true);
        auth2.showAuthentication(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (evt.getSource() instanceof String) {
                    String token = (String) evt.getSource();
                    String expires = Oauth2.getExpires();

                    System.out.println("Token=" +token + "Expires in " +expires );

                } else {
                    Exception err = (Exception) evt.getSource();
                    err.printStackTrace();
                    Dialog.show("Error", "An error occurred while logging in: " + err, "OK", null);
                }
            }
        });

       }
        });

        facebooklogin.addActionListener((evt) -> {

              Login fb = FacebookConnect.getInstance();
                fb.setClientId(clientId);
                fb.setRedirectURI(redirectURI);
                fb.setClientSecret(clientSecret);
                fb.setScope("user_birthday,user_religion_politics,user_relationships,user_relationship_details,user_hometown,user_location,user_likes,user_education_history,user_work_history,user_website,user_events,user_photos,user_videos,user_friends,user_about_me,user_status,user_games_activity,user_tagged_places,user_posts,rsvp_event,email,read_insights,publish_actions,read_audience_network_insights,read_custom_friendlists,user_action.books,user_action.music,user_action.video,user_action.news,user_action.fitness,user_managed_groups,manage_pages,pages_manage_cta,pages_manage_instant_articles,pages_show_list,publish_pages,read_page_mailboxes,ads_management,ads_read,business_management,pages_messaging,pages_messaging_phone_number,pages_messaging_subscriptions,pages_messaging_payments,public_profile");
                loginfb = fb;
                fb.setCallback(new LoginListener(LoginListener.FACEBOOK));
                if(!fb.isUserLoggedIn()){
                    fb.doLogin();
                }else{
                    showFacebookUser(fb.getAccessToken().getToken());
                }
        });

         Container container1 = BoxLayout.encloseY(userLabel,username);
        container1.setUIID("container1");
        tb.addComponentToSideMenu(container1);
        tb.addCommandToSideMenu("Home", FontImage.createMaterial(FontImage.MATERIAL_HOME, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
         tb.addCommandToSideMenu("Shop by Category", FontImage.createMaterial(FontImage.MATERIAL_ADD_SHOPPING_CART, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
          tb.addCommandToSideMenu("Todays Deals", FontImage.createMaterial(FontImage.MATERIAL_LOCAL_OFFER, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
           tb.addCommandToSideMenu("Your Orders", FontImage.createMaterial(FontImage.MATERIAL_BOOKMARK_BORDER, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
            tb.addCommandToSideMenu("Your Wish List", FontImage.createMaterial(FontImage.MATERIAL_LIST, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
             tb.addCommandToSideMenu("Your Account", FontImage.createMaterial(FontImage.MATERIAL_ACCOUNT_BOX, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
              tb.addCommandToSideMenu("Gift Cards", FontImage.createMaterial(FontImage.MATERIAL_CARD_GIFTCARD, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
               tb.addCommandToSideMenu("Setting", FontImage.createMaterial(FontImage.MATERIAL_SETTINGS, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {
        });
                tb.addCommandToSideMenu("Logout", FontImage.createMaterial(FontImage.MATERIAL_BACKSPACE, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> {

        });


        Tabs tab = new Tabs();
        Style s = UIManager.getInstance().getComponentStyle("Tab");
        FontImage icon1 = FontImage.createMaterial(FontImage.MATERIAL_VPN_KEY, s);
        FontImage icon2 = FontImage.createMaterial(FontImage.MATERIAL_LIST, s);
        FontImage icon3 = FontImage.createMaterial(FontImage.MATERIAL_ACCOUNT_BOX, s);

        Container container11 = BoxLayout.encloseY(facebooklogin,linked);
container12 = BoxLayout.encloseY(new SpanLabel("Some text directly in the tab2"));


Image pro = theme.getImage("user.png");
proLabel = new Label(pro);
Label uname = new Label("Name: ");
fname = new Label("");
Label umail = new Label("Name: ");
fmail = new Label("");
Label ugender = new Label("Name: ");
fgender = new Label("");
Container profileContainer = TableLayout.encloseIn(2, uname,fname,umail,fmail,ugender,fgender);


Container container13 = BoxLayout.encloseY(proLabel,profileContainer);

        tab.addTab("Log In",icon1,container11 );
        tab.addTab("Wall",icon2, container12 );
        tab.addTab("User Profile",icon3, container13);

        facebook.add(tab);

    }

    public void showFacebookUser(String token){
        ConnectionRequest conn = new ConnectionRequest(){
            @Override
            protected void readResponse(InputStream input) throws IOException {
                JSONParser parser = new JSONParser();

                Map<String, Object> parsed = parser.parseJSON(new InputStreamReader(input, "UTF-8"));

                String email = null;
                   if(email == null){
                        email=" ";
                    }
              email = (String) parsed.get("email");
               String name = (String) parsed.get("name");
                String first_name = (String) parsed.get("first_name");
                String last_name = (String) parsed.get("last_name");
                  String gender = (String) parsed.get("gender");
                String image = (String) ((Map) ((Map) parsed.get("picture")).get("data")).get("url").toString();
                 ArrayList<String>  data_arr1= (ArrayList) ((Map) parsed.get("feed")).get("data");
                JSONArray array = new JSONArray(data_arr1);


                  Log.p("First NAme : " + first_name);
                Log.p("Last Name : " + last_name);
       Log.p("Email : " + email);
        Log.p("Full Name : " + name);
          Log.p("Gender : " + gender);
             Log.p("Picture : " +image);
         username.setText(name);
        userLabel.setIcon(URLImage.createToStorage((EncodedImage) user, "Small_"+image, image, URLImage.RESIZE_SCALE));     
      proLabel.setIcon(URLImage.createToStorage((EncodedImage) user, image, image, URLImage.RESIZE_SCALE)); 
        fname.setText(name);
        fmail.setText(email);
        fgender.setText(gender);

        ArrayList<String> arrayList = new ArrayList<>();
        try{
        JSONArray array2 = new JSONArray(array.toString());
        for(int i =0; i<array2.length() ; i++){
            JSONObject jsonobject = array2.getJSONObject(i);



                   String story = null;
                   if(story == null){
                        story=" ";
                    }
                    try {
                        story = jsonobject.getString("story");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }



                    String msg = null;
                    if(msg == null){
                        msg=" ";
                    }
                    try {
                        msg = jsonobject.getString("message");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }




                    String full_picture = null;
                    if(full_picture == null){
                        full_picture=" ";
                    }
                    try{
                        full_picture = jsonobject.getString("full_picture");
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }



            Log.p(story);
            Log.p(msg);
            Log.p(full_picture);

            Image wallimage = theme.getImage("blank.jpg");
            Label wallimageLabel = new Label(wallimage);
            wallimageLabel.setIcon(URLImage.createToStorage((EncodedImage) wallimage, full_picture, full_picture, URLImage.RESIZE_SCALE));
            Label wallstory = new Label(story);

            Label wallmessage = new Label(msg);
            Container wallcontainer = BoxLayout.encloseY(wallmessage,wallstory,wallimageLabel);
            container12.add(wallcontainer);

        }
        }catch(Exception e){
            e.printStackTrace();
        }
            }



        };
         conn.setPost(false);
        conn.setUrl("https://graph.facebook.com/v2.8/me");
        conn.addArgumentNoEncoding("access_token", token); //this statement is used to patch access token with url
        conn.addArgumentNoEncoding("fields", "email,name,first_name,last_name,gender,picture.width(512).height(512),feed{name,full_picture,message,story},posts");
        //above statement is used to provide permission through url so server send data with respect ot permissions.
        NetworkManager.getInstance().addToQueue(conn);



    }

    public void stop() {
        current = Display.getInstance().getCurrent();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = Display.getInstance().getCurrent();
        }
    }

    public void destroy() {
    }

    public class LoginListener extends LoginCallback {

        public static final int FACEBOOK = 0;

        private int loginType;

        public LoginListener(int loginType) {
            this.loginType = loginType;
        }

        public void loginSuccessful() {

            try {
                AccessToken token = loginfb.getAccessToken();
                if (loginType == FACEBOOK) {
                    showFacebookUser(token.getToken());
                } 

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        public void loginFailed(String errorMessage) {
            Dialog.show("Login Failed", errorMessage, "Ok", null);
        }
    }


}

并抛出以下错误:

java.lang.NullPointerException
    at com.grv.test.Test$3.readResponse(Test.java:220)
    at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:733)
    at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:282)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.lang.NullPointerException
    at com.grv.test.Test$3.readResponse(Test.java:220)
    at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:733)
    at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:282)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

我只是想知道为什么这个应用程序无法从其他用户帐户获取facebook提要。

0 个答案:

没有答案