发布推文失败 - com.twitter.sdk.android.core.TwitterApiException:403 Forbidden

时间:2015-11-26 17:51:03

标签: android twitter twitter-oauth twitter-fabric

您好我试图使用最新的Twitter apis发布推文,我得到以下异常

11-26 15:59:07.579 19471-19471/com.pestwitter E/TweetUploadService﹕ Post Tweet failed
com.twitter.sdk.android.core.TwitterApiException: 403 Forbidden
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:383)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

这是我的代码

public class MainActivity extends Activity {

private static final String CONSUMER_KEY ="%%%%%%%%%%%%%";
private static final String CONSUMER_SECRET ="%%%%%%%%%%%%%%%%%%%%%%%%";
public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FORESLASH = "/";
private static final String TAG = "BITMAPS SVE";
private TwitterLoginButton loginButton;
private TwitterAuthClient client;
private Button twitter_logout_button;
private Button twitter_posttweet_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
/* TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
client = new TwitterAuthClient();*/
/* TwitterAuthConfig authConfig = new TwitterAuthConfig(CONSUMER_KEY, CONSUMER_SECRET);
Fabric.with(this, new Twitter(authConfig),new Crashlytics());*/
loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
twitter_logout_button = (Button) findViewById(R.id.twitter_logout_button);
twitter_posttweet_button= (Button) findViewById(R.id.twitter_posttweet_button);

 /*   loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            loginButton.performClick();
        }
    });*/


    if(Twitter.getSessionManager().getActiveSession()!=null)
    {
        Log.e("Active sesion exits","Active sesion exits");
        toggleLoginLogout(true);
    }
    else 
    {
        Log.e("NO Active sesion exits","NO Active sesion exits");

        toggleLoginLogout(false);
    }


    loginButton.setCallback(new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            // The TwitterSession is also available through:
            // Twitter.getInstance().core.getSessionManager().getActiveSession()
            if(Twitter.getSessionManager().getActiveSession()!=null)
            {
                Log.e("Active sesion exits","Active sesion exits");
                toggleLoginLogout(true);
            }
            TwitterSession session = result.data;
            // TODO: Remove toast and use the TwitterSession's userID
            // with your app's user model
            String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();



            Bitmap  icon= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            storeImage(icon);



            File myImageFile = new File("/storage/emulated/0/Android/data/com.pestwitter/Files/pestwitter.jpg");

            Uri myImageUri = Uri.fromFile(myImageFile);
            final Card card = new Card.AppCardBuilder(MainActivity.this)
                    .imageUri(myImageUri)
                    .googlePlayId("com.pestwitter")
                    .iPhoneId("123456")
                    .iPadId("123456")
                    .build();

            final Intent tweetIntent = new ComposerActivity.Builder(MainActivity.this)
                    .session(session)
                    .card(card)
                    .createIntent();

            ((Activity) MainActivity.this).startActivity(tweetIntent);



        }

        @Override
        public void failure(TwitterException exception) {
            Log.d("TwitterKit", "Login with Twitter failure", exception);
        }
    });



    twitter_logout_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            logout();
            toggleLoginLogout(false);
        }
    });

    twitter_posttweet_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

private void storeImage(Bitmap image) {
    File pictureFile = getOutputMediaFile();
    Log.e("getAbsoluteFile",pictureFile.getAbsoluteFile().toString());
    Log.e("getPath",pictureFile.getPath().toString());


    if (pictureFile == null) {
        Log.d(TAG,
                "Error creating media file, check storage permissions: ");// e.getMessage());
        return;
    }
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.d(TAG, "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.d(TAG, "Error accessing file: " + e.getMessage());
    }
}

/** Create a File for saving an image or video */
private  File getOutputMediaFile(){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + getApplicationContext().getPackageName()
            + "/Files");



    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
    File mediaFile;
    String mImageName="pestwitter.jpg";
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
    return mediaFile;
}

void  toggleLoginLogout(boolean status)
{
    if(status)
    {
        //twitter_logout_button.setVisibility(View.VISIBLE);
        loginButton.setVisibility(View.INVISIBLE);
        twitter_posttweet_button.setVisibility(View.VISIBLE);
    }
    else
    {
      //  twitter_logout_button.setVisibility(View.GONE);
        loginButton.setVisibility(View.VISIBLE);
        twitter_posttweet_button.setVisibility(View.INVISIBLE);
    }

}
public static Uri resIdToUri(Context context, int resId) {
    return Uri.parse(ANDROID_RESOURCE + context.getPackageName()
            + FORESLASH + resId);
}
private void logout() {


    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();
    Twitter.getSessionManager().clearActiveSession();
    Twitter.logOut();


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // Make sure that the loginButton hears the result from any
    // Activity that it triggered.
    loginButton.onActivityResult(requestCode, resultCode, data);
}
}

有人可以帮我识别问题吗?

PS: - 如果您想要更多信息,请告诉我

0 个答案:

没有答案