I'm trying to share a photo from my Andoid application, on Facebook using the FaceBook SDK. I have done
Facebook fb;
String APP_ID="xxxxx";//xxxxx: is my app id
private static final String[] PERMISSIONS = new String[] { "publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
mShareButton= (Button) findViewById(R.id.share_button);
mShareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
restoreCredentials(fb);
messageToPost = "Hello Everyone.";
if (!fb.isSessionValid()) {
loginAndPostToWall();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
public void loginAndPostToWall() {
fb.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH,
new LoginDialogListener());
}
public void postPhotoToWall() {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.melody);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] data = stream.toByteArray();
Bundle parameters = new Bundle();
parameters.putString("message", "Message");
parameters.putByteArray("picture", data);
parameters.putString("caption", "test");
try {
Log.i("Tests", "got response: " );
fb.request("me/feed");
Log.i("Tests", "got response: " );
String response = fb.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") || response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Photo posted to your facebook wall!");
}
} catch (Exception e) {
showToast("Failed to post photo to your facebook wall!");
e.printStackTrace();
}
}
When I click on the share button I can connect just with my account from which I get the App ID. Also I can't post the photo on the wall: I just get "Failed to post photo to your facebook wall!". I can't figure out the problem.Any help please.
Related
I am trying to upload a file to dropbox via AsyncTask and I am gett the window leaked error:
05-16 16:05:53.523: E/WindowManager(4528): Activity com.example.wecharades.ShowVideo has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#2be59ea8 that was originally added here
I looked here and understand it is because my activity is exited and the progressdialog is still there. I can't understand why my activity gets exited. it is just a screen with a button to press to upload the file. I am using almost an identical code to download a file from dropbox and it works perfect.
Any suggestions?
thanks in advance!
public class UploadFile extends AsyncTask<Void, Long, Boolean> {
DropboxAPI<AndroidAuthSession> dDBApi;
Context dContext;
protected final ProgressDialog uDialog;
private long dFileLen;
private String SAVE_PATH;
private String mErrorMsg;
public UploadFile(Context context,DropboxAPI<AndroidAuthSession> mDBApi, String path) {
dDBApi=mDBApi;
dContext=context;
SAVE_PATH = path;
uDialog = new ProgressDialog(context);
uDialog.setMax(100);
uDialog.setMessage("Uploading Video Charade");
uDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
uDialog.show();
}
#Override
protected Boolean doInBackground(Void... params) {
FileInputStream inputStream = null;
try {
File file = new File(ShowVideo.path);
inputStream = new FileInputStream(file);
Entry newEntry = dDBApi.putFileOverwrite("/GAMES/GAME_BETWEEN_USER_A_USER_B/" + "PresentVideo.mp4", inputStream, file.length(), new ProgressListener() {
#Override
public long progressInterval() {
// Update the progress bar every half-second
return 500;
}
#Override
public void onProgress(long bytes, long total) {
dFileLen = total;
publishProgress(bytes);
}
});
} catch (DropboxUnlinkedException e) {
// This session wasn't authenticated properly or user unlinked
mErrorMsg = "This app wasn't authenticated properly.";
} catch (DropboxFileSizeException e) {
// File size too big to upload via the API
mErrorMsg = "This file is too big to upload";
} catch (DropboxPartialFileException e) {
// We canceled the operation
mErrorMsg = "Upload canceled";
} catch (DropboxServerException e) {
// Server-side exception. These are examples of what could happen,
// but we don't do anything special with them here.
if (e.error == DropboxServerException._401_UNAUTHORIZED) {
// Unauthorized, so we should unlink them. You may want to
// automatically log the user out in this case.
} else if (e.error == DropboxServerException._403_FORBIDDEN) {
// Not allowed to access this
} else if (e.error == DropboxServerException._404_NOT_FOUND) {
// path not found (or if it was the thumbnail, can't be
// thumbnailed)
} else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
// user is over quota
} else {
// Something else
}
// This gets the Dropbox error, translated into the user's language
mErrorMsg = e.body.userError;
if (mErrorMsg == null) {
mErrorMsg = e.body.error;
}
} catch (DropboxIOException e) {
// Happens all the time, probably want to retry automatically.
mErrorMsg = "Network error. Try again.";
} catch (DropboxParseException e) {
// Probably due to Dropbox server restarting, should retry
mErrorMsg = "Dropbox error. Try again.";
} catch (DropboxException e) {
// Unknown error
mErrorMsg = "Unknown error. Try again.";
} catch (FileNotFoundException e) {
}
catch (Exception e) {
System.out.println("Something went wrong: " + e);
}
finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
Log.d("TAG", "IOException" + e.getMessage());
}
}
Log.d("ErrorMsg", mErrorMsg);
}
return null;
}
#Override
protected void onProgressUpdate(Long... progress) {
int percent = (int)(100.0*(double)progress[0]/dFileLen + 0.5);
uDialog.setProgress(percent);
}
#Override
protected void onPostExecute(Boolean result) {
uDialog.dismiss();
super.onPostExecute(result);
Log.d("TAG","UDialog Should be dismissed");
}
}
Here is my Activity class from where i call the UploadFile:
The methods buidSession and clearKeys are not yet used.
public class ShowVideo extends Activity implements OnClickListener {
/** Dropbox Key and AccessType Information*/
final static private String APP_KEY = "XXXXXXXXXXXX";
final static private String APP_SECRET = "XXXXXXXXXXXX";
final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
/**--------------------------------------------------------------*/
private DropboxAPI<AndroidAuthSession> mDBApi;
UploadFile upload;
static String path = "";
public static String fileName;
private VideoView ww;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //Forces landscape orientation which is what the camera uses.
setContentView(R.layout.showvideo);
Button yesButton = (Button) findViewById(R.id.yesButton);
Button noButton = (Button) findViewById(R.id.NoButton);
yesButton.setOnClickListener(this);
noButton.setOnClickListener(this);
ww = (VideoView) findViewById(R.id.satisfiedVideoView);
path = getRealPathFromURI(CaptureVideo.uriVideo);
fileName = getFileNameFromUrl(path);
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
if(!mDBApi.getSession().isLinked())
mDBApi.getSession().startAuthentication(ShowVideo.this);
}
private void playVideo(){
ww.setVideoURI(CaptureVideo.uriVideo);
ww.setMediaController(new MediaController(this));
ww.start();
ww.requestFocus();
}
public static String getFileNameFromUrl(String path) {
String[] pathArray = path.split("/");
return pathArray[pathArray.length - 1];
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.yesButton){
UploadFile upload = new UploadFile(ShowVideo.this,mDBApi,path);
upload.execute();
//if(upload.getStatus() == upload.){
//Intent intentHome = new Intent(ShowVideo.this, StartScreen.class);
//startActivity(intentHome);
//}
}
if(v.getId() == R.id.NoButton){
File file = new File(path);
boolean deleted = false;
deleted = file.delete();
Log.d("TAG", Boolean.toString(deleted));
Intent intent = new Intent(ShowVideo.this, CaptureVideo.class);
startActivity(intent);
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
/**DROPBOX-METHOD------------------------------------------*/
#Override
protected void onResume() {
super.onResume();
AndroidAuthSession session = mDBApi.getSession();
// The next part must be inserted in the onResume() method of the
// activity from which session.startAuthentication() was called, so
// that Dropbox authentication completes properly.
if (session.authenticationSuccessful()) {
try {
// Mandatory call to complete the auth
session.finishAuthentication();
// Store it locally in our app for later use
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
//setLoggedIn(true);
} catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
Log.i("TAG", "Error authenticating", e);
}
}
}
/**
* Shows keeping the access keys returned from Trusted Authenticator in a local
* store, rather than storing user name & password, and re-authenticating each
* time (which is not to be done, ever).
*
* #return Array of [access_key, access_secret], or null if none stored
*/
private String[] getKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(ACCESS_KEY_NAME, null);
String secret = prefs.getString(ACCESS_SECRET_NAME, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
/**
* Shows keeping the access keys returned from Trusted Authenticator in a local
* store, rather than storing user name & password, and re-authenticating each
* time (which is not to be done, ever).
*/
private void storeKeys(String key, String secret) {
// Save the access key for later
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(ACCESS_KEY_NAME, key);
edit.putString(ACCESS_SECRET_NAME, secret);
edit.commit();
}
private void clearKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0], stored[1]);
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
}
return session;
}
private void showToast(String msg) {
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
error.show();
}
}
Try this, in your constructor you dont get the context like this:
dContext=context.getApplicationContext();
but try passing the activity from which you start your asynctask
UploadFile upFile = new UploadFile( ActivityName.this, mDBApi, path);
so in your constructor you have now only:
dContext=context;
call super.onPostExexute(result) after uDialog.dismiss();
#Override
protected void onPostExecute(Boolean result) {
uDialog.dismiss();
Log.d("TAG","UDialog Should be dismissed");
super.onPostExecute(result);
}
EDIT:
ok, I think you should call this code
if(v.getId() == R.id.NoButton){
File file = new File(path);
boolean deleted = false;
deleted = file.delete();
Log.d("TAG", Boolean.toString(deleted));
Intent intent = new Intent(ShowVideo.this, CaptureVideo.class);
startActivity(intent);
}
in the onPostExecute trigger, because it's executed without waiting the AsyncTask to finish. This means you will have to pass the value of the v.getId() so you can achive the same functionality.
Hi i did one application here i need to share my score on twiter,i did using below code my score is posing fine,but now i need to share score along with app icon.but i dont know how to share that image along with text can any one help me,thankyou
TestPost.class:
public class TestPost extends Activity {
String review;
private TwitterApp mTwitter;
private String username = "";
private boolean postToTwitter = false;
private static final String twitter_consumer_key = "";
private static final String twitter_secret_key = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
Button postBtn = (Button) findViewById(R.id.button1);
final EditText reviewEdit = (EditText) findViewById(R.id.revieew);
postBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
review = reviewEdit.getText().toString();
postToTwitter = true;
onTwitterClick();
}
});
mTwitter = new TwitterApp(this, twitter_consumer_key,twitter_secret_key);
mTwitter.setListener(mTwLoginDialogListener);
if (mTwitter.hasAccessToken()) {
username = mTwitter.getUsername();
username = (username.equals("")) ? "No Name" : username;
}
}
private void postToTwitter(final String review) {
new Thread() {
#Override
public void run() {
int what = 0;
try {
mTwitter.updateStatus(review);
} catch (Exception e) {
what = 1;
}
mHandler.sendMessage(mHandler.obtainMessage(what));
}
}.start();
}
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
String text = (msg.what == 0) ? "Posted to Twitter" : "Post to Twitter failed";
Toast.makeText(TestPost.this, text, Toast.LENGTH_SHORT).show();
}
};
private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
#Override
public void onComplete(String value) {
username = mTwitter.getUsername();
username = (username.equals("")) ? "No Name" : username;
postToTwitter = true;
postToTwitter(review);
Toast.makeText(TestPost.this, "Connected to Twitter as " + username, Toast.LENGTH_LONG).show();
}
#Override
public void onError(String value) {
Toast.makeText(TestPost.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
}
};
private void onTwitterClick() {
if (mTwitter.hasAccessToken()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete current Twitter connection?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mTwitter.resetAccessToken();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
} else {
mTwitter.authorize();
}
}
}
This is how I upload an Image along with text to the logged in Twitter User's account. I make use of the TwitPic API for that. You will have to login and register a developer account there.
The solution can be thought of as a two step solution.
First, when the user clicks the post button, I first grab the Image and upload to TwitPic. From there, I grab the URL that is returned by TwitPic (String url = upload.upload(finalFile);).
In step two of this code, in a String instance (String finalStatusWithURL), I grab the content of an EditText and then append the URL from step 1 to it. With this done, the post is finally posted to Twitter.
Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.setOAuthAccessToken(twit_access_token)
.setOAuthAccessTokenSecret(twit_access_token_secret)
.setMediaProviderAPIKey(TWIT_PIC_API)
.build();
// SET THE FILE PATH
File finalFile = new File(getRealPathFromURI(initialURI));
// THIS IS IMPORTANT. TWITPIC NEEDS THE ACTUAL PATH ON THE DEVICE. JUST THE URI DOES NOT WORK!!!!
ImageUpload upload = new ImageUploadFactory(conf).getInstance(MediaProvider.TWITPIC);
String url = upload.upload(finalFile);
Log.e("TWITTER URL RESPONSE", url);
// NOW, UPLOAD TO TWITTER
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(YOUR_TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(YOUR_TWITTER_CONSUMER_SECRET);
AccessToken accessToken = new AccessToken(your_twit_access_token, your_twit_access_token_secret);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
String finalStatusWithURL = null;
if (finalStatusMessage.trim().length() > 0) {
finalStatusMessage = editStatusUpdate.getText().toString();
finalStatusWithURL = finalStatusMessage + ":\n " + url;
} else {
finalStatusWithURL = url;
}
twitter4j.Status response = twitter.updateStatus(finalStatusWithURL);
Log.e("TWITTER RESPONSE", response.getText());
This is a method to get the real path of the image you want to upload:
// HELPER METHOD TO GET REAL PATH FOR THE SELECTED IMAGE
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
Hope this helps. This is production code from an app of mine.
UPDATED:
Change this in the code:
File finalFile = new File(getRealPathFromURI(initialURI));
To this:
Uri tempUri = getImageUri(getApplicationContext(), icon);
File finalFile = new File(getRealPathFromURI(tempUri));
And the helper method for Uri tempUri = getImageUri(getApplicationContext(), bmpFinal);:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
You will still need to pass a Bitmap object. To convert your app icon to a Bitmap, use this:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
UPDATED 2:
A little while after you asking and me answering your question, Twitter wrapped up a developer meet in SF. They have introduced a few new Cards which I think may be of use to your specific use-case. It is fresh off the block and I haven't gotten around to testing/using it. But if it fits your purpose, take a look at these links:
http://www.engadget.com/2013/04/02/twitter-cards-apps-products-photo-galleries/ (Where I found out about the new feature)
https://dev.twitter.com/blog/mobile-app-deep-linking-and-new-cards (The official Twitter blog)
https://dev.twitter.com/cards (Developer page for details of the features and how to use them)
I have been Using this SDK http://www.easyfacebookandroidsdk.com/ to integrate Facebook in my App.
I only want to update a statur from my App. It worked great using this class in Android API 10. Running it on phone with API 15 gives me a android.os.NetworkOnMainThreadException. here my code:
package com.my.wod;
import android.app.Activity;
import android.content.Context;
import com.main.xfit.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;
public class PostFaceBookStatus extends Activity {
private static final String APP_ID = "ID OF MY APP";
private static final String[] PERMISSIONS = {"publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
private Facebook facebook;
private String messageToPost;
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.facebook);
String facebookMessage = getIntent().getStringExtra("facebookMessage");
Toast.makeText(getApplicationContext(), facebookMessage, Toast.LENGTH_LONG).show();
if (facebookMessage == null){
facebookMessage = "SUNDAY 120506 2012 CrossFit Games Regional: Individual Event 4 For time: 135 pound Back squat, 50 reps 40 Pull-ups 135 pound Shoulder-to-overhead, 30 reps 85 pound Front squat, 50 reps 40 Pull-ups 85 pound Shoulder-to-overhead, 30 reps 65 pound Overhead squat, 50 reps 40 Pull-ups 65 pound Shoulder-to-overhead, 30 reps";
}
//messageToPost = facebookMessage;
messageToPost = "Post random shit" ;
}
public void doNotShare(View button){
finish();
}
public void share(View button){
if (! facebook.isSessionValid()) {
loginAndPostToWall();
}
else {
postToWall(messageToPost);
}
}
public void loginAndPostToWall(){
facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
public void postToWall(String message){
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
showToast("Blank response.");
}
else {
showToast("Message posted to your facebook wall!");
}
finish();
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null){
postToWall(messageToPost);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
finish();
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
finish();
}
}
private void showToast(String message){
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
Any ideas?
You can't run network operations on the UI thread, and if you think it is is the API code doing it, then you'll have to wrap the API call's in a Thread.
Looks like this line is using the network:
String response = facebook.request("me/feed", parameters, "POST");
You need to do long running tasks on a worker thread.
You used to get an ANR (Activity Not Responsding) but ICS is a little cleverer and kicks you off before this happens.
Get reading:
http://developer.android.com/guide/practices/responsiveness.html
http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask < This is one possible alternative answer
To make it work for API level 10 and higher i just employed and AsyncTask and it Worked!!
Use the below code:
//make this global declaration
private Facebook mFacebook;
private class FbLoginAsync extends AsyncTask<Void, Void, User> {
#Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(ConstantsCtc.DIALOG_PROGRESS);
}
#Override
protected User doInBackground(Void... params) {
// facebooklogin();
return getFacebookData();
}
#Override
protected void onPostExecute(User u) {
super.onPostExecute(u);
// your facebook information is in User bean 'u'.
}
}
public User getFacebookData() {
User u = new User();
try {
JSONObject jsonME = null;
JSONObject jsonfrnd = null;
try {
jsonME = Util.parseJson(mFacebook.request("me"));
// jsonfrnd =
// Util.parseJson(mFacebook
// .request("me/friends"));
} catch (FacebookError e) {
e.printStackTrace();
u = null;
}
Log.w("fbjsonMe", " *\n " + jsonME);
// Log.e("fbjsonfrnds", " *\n " + jsonfrnd);
String mUserId = jsonME.getString("id");
String mUserName = jsonME.getString("first_name");
String mUseremail = jsonME.getString("email");
String birthday = jsonME.getString("birthday");
String gender = jsonME.getString("gender");
String full_name = jsonME.getString("name");
// Log.w("UserName", mUserName + " * " + mUseremail + " *\n "
// + jsonME);
// calculate age
String ag[] = birthday.split("/");
int y = Integer.parseInt(ag[2]);// get birth year
final Calendar cal = Calendar.getInstance();
int currentyear = cal.get(Calendar.YEAR);
int age = currentyear - y;
if (gender.equalsIgnoreCase("male"))
gender = "0";
else
gender = "1";
u.setUserId(mUserId);
u.setUserFullName(full_name);
u.setEmail(mUseremail);
u.setAge(age + "");
u.setGender(gender);
} catch (Exception e) {
e.printStackTrace();
u = null;
}
return u;
}
// then make a call to below method form your activity's button click or
// else
public void facebookLogin() {
this.mFacebook = new Facebook(appId);// give your appId
if (mFacebook.isSessionValid()) {
new FbLoginAsync().execute();
} else {
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
#Override
public void onAuthSucceed() {
new FbLoginAsync().execute();
}
#Override
public void onAuthFail(String error) {
showDialogCustome("Error in Facebook Login, please try again later");
Log.e("onAuthFail", "onAuthFail");
}
};
SessionEvents.addAuthListener(listener);
mFacebook.authorize(this.activity, this.permissions,
Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
}
I hope this solves your problem. let me know
I have this code running in my app and it is trying to post text and an image to a users wall. At the moment it is only posting the text. I think I have missed something simple, but would appreciate another pair of eyes check everything or another sample.
Any help is greatly appreciated.
Bundle bundle = new Bundle();
bundle.putString("message", "test update"); //'message' tells facebook that you're updating your status
bundle.putString(Facebook.TOKEN,accessToken);
bundle.putString("attachment", "{\"name\":\"My Test Image\","
+"\"href\":\""+"http://www.google.com"+"\","
+"\"media\":[{\"type\":\"image\",\"src\":\""+"http://www.google.com/logos/mucha10-hp.jpg"+"\",\"href\":\""+"http://www.google.com"+"\"}]"
+"}");
+"}");
//tells facebook that you're performing this action on the authenticated users wall, thus
// it becomes an update. POST tells that the method being used is POST
String response = facebook.request("me/feed",bundle,"POST");
Hope this will be work for you
Create class for facebook varible utility
import android.app.Application;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
public class Utility extends Application{
public static Facebook mFacebook;
public static AsyncFacebookRunner mAsyncRunner;
public static String userUID;
public static final String ICON_URL = "http://i.imgur.com/6G1b7.png";
}
Now mathod used to post image to facebook wall
public void postOnFacebookPicture(final Bitmap bitmap) {
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
Utility.mFacebook.setAccessToken(access_token);
}
if (expires != 0) {
Utility.mFacebook.setAccessExpires(expires);
}
if (!Utility.mFacebook.isSessionValid()) {
showErrorDialog(
"Facebook Account is not configure,Setting Facebook Account?",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
// Move to setting the facebook account
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
} else {
new Thread() {
#Override
public void run() {
int what = 0;
try {
String accessToken = mPrefs.getString("access_token",
null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, bos);
byte[] pictureData = bos.toByteArray();
Bundle bundle = new Bundle();
bundle.putByteArray("facebookPictureData", pictureData);
bundle.putString(Facebook.TOKEN, accessToken);
Utility.mFacebook.request("me/photos", bundle, "POST");
} catch (Exception e) {
what = 1;
}
mHandler.sendMessage(mHandler.obtainMessage(what));
}
}.start();
}
}
For Posting Text and Image in Facebook wall, check this link:
You can insert Image using Media attachment.
I'm experiencing some problems regarding Twitter OAuth within an android activity. I read a lot of tutorials and example code, but I'm still not able to receive the access token.
Everytime I try to authorize I'm getting this OAuthNotAuthorizedException:
oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
I googled a lot and it felt like I read thousands of solutions, but none of them worked out for me. Hope you can help me with this one (and I'm not bothering your with the same old newbie question! ;) )
Here is my complete activity code:
package de.ownor.moremote;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MakeTweet extends Activity {
public static final String CONSUMER_KEY = "Y0iYMkUgNX8kKgvDjzFgg";
public static final String CONSUMER_SECRET = "nuBZd8UGml5cjbm94Hf6dWOwIByrisZWpSnqODaUB5Q";
public static final String CALLBACK_URL = "de.ownor.moremote://twitter";
private static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
private static final String AUTH_URL = "https://api.twitter.com/oauth/authorize";
public static final String PREFS_NAME = "TwitterLogin";
public static final String TAG = "moremote - MakeTweet";
private Twitter twitter;
private CommonsHttpOAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;
#Override
protected void onCreate(Bundle savedInstanceState) {
System.setProperty("http.keepAlive", "false");
super.onCreate(savedInstanceState);
setContentView(R.layout.make_tweet);
twitter = null;
if (!checkForSavedLogin()) {
askOAuth();
}
getConsumerProvider();
}
#Override
protected void onResume() {
super.onResume();
System.out.println("RESUMING!!");
if (this.getIntent() != null && this.getIntent().getData() != null) {
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri
.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
// this will populate token and token_secret in consumer
// /////////////////////////////
// exception is thrown here! //
// /////////////////////////////
provider.retrieveAccessToken(consumer, verifier);
// Get Access Token and persist it
AccessToken a = new AccessToken(consumer.getToken(),
consumer.getTokenSecret());
storeAccessToken(a);
// initialize Twitter4J
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
((ApplicationEx) getApplication()).twitter = twitter;
} catch (Exception e) {
// Log.e(APP, e.getMessage());
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
}
}
private void askOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(REQUEST_URL,
ACCESS_TOKEN_URL, AUTH_URL);
provider.setOAuth10a(true);
String authUrl = provider.retrieveRequestToken(consumer,
CALLBACK_URL);
Toast.makeText(this, "Please authorize this app!",
Toast.LENGTH_LONG).show();
setConsumerProvider();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catch (Exception e) {
Log.e(TAG, "Error in askOAuth");
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
private Boolean checkForSavedLogin() {
// Get Access Token and persist it
AccessToken a = getAccessToken();
if (a == null) {
Log.d(TAG, "No saved Login found");
return false;
}
// initialize Twitter4J
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
((ApplicationEx) getApplication()).twitter = twitter;
return true;
}
private AccessToken getAccessToken() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
String token = settings.getString("accessTokenToken", "");
String tokenSecret = settings.getString("accessTokenSecret", "");
if (token != null && tokenSecret != null && !"".equals(tokenSecret)
&& !"".equals(token)) {
return new AccessToken(token, tokenSecret);
}
return null;
}
private void storeAccessToken(AccessToken a) {
SharedPreferences settings = Preferences.getPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("accessTokenToken", a.getToken());
editor.putString("accessTokenSecret", a.getTokenSecret());
editor.commit();
}
private void getConsumerProvider() {
CommonsHttpOAuthProvider p = ((ApplicationEx) getApplication()).provider;
if (p != null) {
provider = p;
}
CommonsHttpOAuthConsumer c = ((ApplicationEx) getApplication()).consumer;
if (c != null) {
consumer = c;
}
}
private void setConsumerProvider() {
if (provider != null) {
((ApplicationEx) getApplication()).provider = provider;
}
if (consumer != null) {
((ApplicationEx) getApplication()).consumer = consumer;
}
}
}
The exception is thrown in the onResume()-Method. I marked the exact line. I really hope anyone can help me here. If you need further information just scream!
Thanks!
Simon.
I finally managed to get the OAuth running. Unless I don't know the exact error. I guess it was some issue with the consumer.
Here is my running code:
onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twitter);
pref = Preferences.getPreferences(this);
consumer = new CommonsHttpOAuthConsumer(Key.TW_CONSUMER_KEY,
Key.TW_CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(Key.TW_REQUEST_TOKEN_URL,
Key.TW_ACCESS_TOKEN_URL, Key.TW_AUTHORIZE_URL);
provider.setOAuth10a(true);
[...]
if (getIntent().getData() == null) {
checkForSavedLogin();
}
}
onResume:
protected void onResume() {
super.onResume();
Uri uri = getIntent().getData();
if (uri != null
&& Key.TW_CALLBACK_URI.getScheme().equals(uri.getScheme())) {
String token = pref.getString(Key.TW_REQUEST_TOKEN, null);
String secret = pref.getString(Key.TW_REQUEST_SECRET, null);
try {
if (!(token == null || secret == null)) {
consumer.setTokenWithSecret(token, secret);
}
String otoken = uri.getQueryParameter(OAuth.OAUTH_TOKEN);
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Assert.assertEquals(otoken, consumer.getToken());
provider.retrieveAccessToken(consumer, verifier);
token = consumer.getToken();
secret = consumer.getTokenSecret();
saveAuthInformation(token, secret);
// Delete request information
saveRequestInformation(null, null);
if (!(token == null || secret == null)) {
Log.d(TAG, "Twitter login found!");
consumer.setTokenWithSecret(token, secret);
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Key.TW_CONSUMER_KEY,
Key.TW_CONSUMER_SECRET);
twitter.setOAuthAccessToken(new AccessToken(token, secret));
}
} catch (Exception e) {
Log.d(TAG, "Couldn't receive token" + e.getMessage());
e.printStackTrace();
}
}
[...]
}
other methods:
private void checkForSavedLogin() {
if (pref.contains(Key.TW_USER_TOKEN)
&& pref.contains(Key.TW_USER_SECRET)) {
token = pref.getString(Key.TW_USER_TOKEN, null);
secret = pref.getString(Key.TW_USER_SECRET, null);
if (!(token == null || secret == null)) {
Log.d(TAG, "Twitter login found!");
consumer.setTokenWithSecret(token, secret);
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Key.TW_CONSUMER_KEY,
Key.TW_CONSUMER_SECRET);
twitter.setOAuthAccessToken(new AccessToken(token, secret));
}
} else {
Log.d(TAG, "No Twitter login saved - asking for OAuth");
askOAuth();
}
}
private void askOAuth() {
try {
String authUrl = provider.retrieveRequestToken(consumer,
Key.TW_CALLBACK_URI.toString());
saveRequestInformation(consumer.getToken(),
consumer.getTokenSecret());
Toast.makeText(this, "Please authorize this app!",
Toast.LENGTH_LONG).show();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(authUrl)));
} catch (OAuthException e) {
e.printStackTrace();
}
}
private void saveRequestInformation(String token, String secret) {
// null means to clear the old values
SharedPreferences.Editor editor = pref.edit();
if (token == null) {
editor.remove(Key.TW_REQUEST_TOKEN);
} else {
editor.putString(Key.TW_REQUEST_TOKEN, token);
}
if (secret == null) {
editor.remove(Key.TW_REQUEST_SECRET);
} else {
editor.putString(Key.TW_REQUEST_SECRET, secret);
}
editor.commit();
}
private void saveAuthInformation(String token, String secret) {
pref = Preferences.getPreferences(this);
Editor editor = pref.edit();
editor.putString(Key.TW_USER_TOKEN, token);
editor.putString(Key.TW_USER_SECRET, secret);
editor.commit();
}
Feel free to use this code... :)
¿Are you using
mHttpOauthprovider = new DefaultOAuthProvider("https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
with HTTPS protocol?