简体   繁体   中英

Strange android-facebook share button

I have implemented facebook sdk into my android project. I am using a share button, which should post a new message to the users timeline. Everything is working Ok, but I wanted to try the app without having installed Facebook app on my phone.

I have uninstalled facebook, cleared cache, cleared all data, removed "Facebook account" from settings and restarted the phone. After that, I pressed the share button in my application and the post was on my timeline! How this could be possible? I have uninstalled everything!

This is the code. I don't know if this will help with something.

Fire everyting

@Override
public void onClick(View v)
{
   switch(v.getId())
   {
      case R.id.btnFacebook:
         Session.openActiveSession(this, true, new Session.StatusCallback() {

                    @Override
                    public void call(Session session, SessionState state, Exception exception) {
                        if (session.isOpened()){
                            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                                @Override
                                public void onCompleted(GraphUser user, Response response) {
                                    if (user != null){                              
                                        publishStory();
                                    }
                                }
                            });
                        }
                    }
                });

      break;
   }
}

private void publishStory() {
    Session session = Session.getActiveSession();

    if (session != null){

        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(this, PERMISSIONS);
        session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }

        Bundle postParams = new Bundle();
        postParams.putString("name", "Something...");
        postParams.putString("caption", "My caption...");
        postParams.putString("description", "Awesomedescription");
        postParams.putString("link", "http://www.domain.com");
        postParams.putString("picture", "http://www.domain.com/image.png");

        Request.Callback callback= new Request.Callback() {
            public void onCompleted(Response response) {
                JSONObject graphResponse = response
                                           .getGraphObject()
                                           .getInnerJSONObject();
                String postId = null;
                try {
                    postId = graphResponse.getString("id");

                        Log.i("DEB", " SUCCESS POSTED TIMELINE ! ");


                } catch (JSONException e) {}
                FacebookRequestError error = response.getError();
                if (error != null) {
                    } else {
                        Toast.makeText(getApplicationContext(), 
                             postId,
                             Toast.LENGTH_LONG).show();
                }
            }
        };

        Request request = new Request(session, "me/feed", postParams, 
                              HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }

}

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

The Facebook app and your app are not tied together at all. Once your app gets an access token, it's yours to keep and reuse (even if you delete the Facebook app).

The Session class in the SDK will automatically cache the token for reuse (that way, when you open a new Session, it doesn't ask the user for permissions again). If you want to log the user out from your app, you should call session.closeAndClearTokenInformation() when your app closes (or when the user chooses to log out).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM