I am Upload video from my application and get the information of that video (likes,comments)
Problem 1
I have done uploading but when i get video information it throws me error
Error
{"error":{"message":"Cannot specify type in both the path and query parameter.","type":"OAuthException","code":2500}}
I am using Facebook SDK 3.8
Problem 2
When i upload video it Only visible in my account but not visible from other account even though it is public
Code For Upload Video
private void Upload_Video() {
mDialog = new ProgressDialog(AndroidFacebookConnectActivity.this);
mDialog.setMessage("Uploding video...");
mDialog.show();
String path="/mnt/sdcard/abc/Mirror.mp4";
if (new File(path).exists()) {
try {
Bundle param;
try {
InputStream is = new FileInputStream(path);
byte[] data = readBytes(is);
param = new Bundle();
param.putString("title", "Test 2");
param.putString("message", "uploaded");
param.putByteArray("video.mov", data);
param.putString("contentType", "video/quicktime");
mAsyncRunner.request("kmavadhiya/videos", param, "POST",new FBRequestListener(), null);
Toast.makeText(getApplicationContext(), "Uploading...", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "No videos found in sdcard ",Toast.LENGTH_SHORT).show();
}
}
public byte[] readBytes(InputStream inputStream) throws IOException {
// This dynamically extends to take the bytes you read.
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// This is storage overwritten on each iteration with bytes.
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
// We need to know how may bytes were read to write them to the byteBuffer.
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
// And then we can return your byte array.
return byteBuffer.toByteArray();
}
Upload Request Listner
public class FBRequestListener implements RequestListener {
#Override
public void onComplete(String response, Object state) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(AndroidFacebookConnectActivity.this, "Upload Video Successfully...",Toast.LENGTH_LONG).show();
mDialog.dismiss();
}
});
Log.e("response", response);
try {
JSONObject jObject = new JSONObject(response);
String Id = (String) jObject.get("id");
Log.d("Video Id = ",""+Id);
Bundle param = new Bundle();
param.putString("type", "uploaded");
mAsyncRunner.request("kmavadhiya/videos/"+Id, param, "GET",new VideoDataRequestListener(), null);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state) {
e.printStackTrace();
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
Log.e("", "onFileNotFoundException");
e.printStackTrace();
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
Log.e("", "onMalformedURLException");
e.printStackTrace();
}
#Override
public void onFacebookError(FacebookError e, Object state) {
Log.e("", "onFacebookError");
e.printStackTrace();
}
}
Video Data Listner
public class VideoDataRequestListener implements RequestListener {
#Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
Log.e("response", response);
}
#Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
// TODO Auto-generated method stub
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
If I changed This Line
mAsyncRunner.request("kmavadhiya/videos/"+Id, param, "GET",new VideoDataRequestListener(), null);
With
mAsyncRunner.request("kmavadhiya/videos/", param, "GET",new VideoDataRequestListener(), null);
** I got Blank Response**
Reference
Link 1
Graph Api Reference Link
I Forget To Request Permission At Login time So Its Authentication Error
Add This In Login click
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("basic_info","user_about_me","email","video_upload","user_videos")) // Permission List That You Want To Get
.setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
Reference
Permission List
https://developers.facebook.com/docs/facebook-login/access-tokens/
Set Permission Reference
https://developers.facebook.com/docs/android/login-with-facebook#permissions
Related
I want facebook profile information in my code. This code works Log.e("in try start", "tryyyyyyyyy"); until here but after that not even single log is executed.
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
public void loginToFacebook() {
// mPrefs = getPreferences(SharedPreferences.);
// String access_token = mPrefs.getString("access_token", null);
//long expires = mPrefs.getLong("access_expires", 0);
// if (access_token != null) {
// facebook.setAccessToken(access_token);
// }
// if (expires != 0) {
// facebook.setAccessExpires(expires);
// }
if (!facebook.isSessionValid()) {
facebook.authorize(getActivity(),
new String[] { "email", "publish_actions" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
Toast.makeText(getActivity(), "hiiiiii", Toast.LENGTH_SHORT).show();
//mPrefs=getSharedPreferences("data", getActivity().MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
Log.e("getProfileInformation entry", "getProfileInformation");
getProfileInformation();
}
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
});
}
}
public void getProfileInformation() {
Toast.makeText(getActivity(), "byeeeeeee", Toast.LENGTH_SHORT).show();
Log.e("getProfileInformation start", "getProfileInformation");
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
Log.e("in try start", "tryyyyyyyyy");
JSONObject profile = new JSONObject(json);
// getting name of the user
Log.d("profile", ""+profile);
fb_name = profile.getString("name");
// getting email of the user
fb_email = profile.getString("email");
Log.d("fb_name", "naem"+fb_name+"emial"+fb_email);
//fb_login=true;
// fb_Image = getUserPic(fb_email);
// LoginFuction();
} catch (JSONException e) {
e.printStackTrace();
Log.e("catchhhhhh", ""+e.getMessage());
}
}
public Bitmap getUserPic(String userID) {
String imageURL;
Bitmap bitmap = null;
Log.d("TAG", "Loading Picture");
imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
} catch (Exception e) {
Log.d("TAG", "Loading Picture FAILED");
e.printStackTrace();
}
return bitmap;
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
This code does not give me any name or emailId.
-Hello Abhishek !
- I have tried using Facebook sdk4.+ and i am getting profile info perfectly.
-Firs of all add below code into your oncreate method before setcontentview
FacebookSdk.sdkInitialize(getApplicationContext());
-Then Create you Callbackmanager using below code:-
callbackManager = CallbackManager.Factory.create();
-Add Permissions using below code:-
permission.add("publish_actions");
-Below code is used for Login
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
act,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
if (!TextUtils.isEmpty(object.toString())) {
try {
JSONObject jresJsonObject = new JSONObject(object.toString());
String id = "", name = "", gender = "";
if (!(jresJsonObject.isNull("id"))) {
id = jresJsonObject.getString("id");
}
if (!(jresJsonObject.isNull("gender"))) {
gender = jresJsonObject.getString("gender");
if (gender.equals("male")) {
gender = "0";
} else {
gender = "1";
}
}
if (!(jresJsonObject.isNull("name"))) {
name = jresJsonObject.getString("name");
}
} catch (Exception e) {
}
}
Log.e("graphrequest", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,gender,link");
request.setParameters(parameters);
request.executeAndWait();
}
#Override
public void onCancel() {
Log.i("", "Access Token:: " + "loginResult.getAccessToken()");
}
#Override
public void onError(FacebookException exception) {
Log.i("", "Access Token:: " + "loginResult.getAccessToken()");
}
});
LoginManager.getInstance().logInWithPublishPermissions(this, permission);
-Last but no least add below code in your OnActivitResult
callbackManager.onActivityResult(requestCode, resultCode, data);
NOTE:- This is using latest Facebook sdk
-Please inform me if it is not usefull or you are still getting issue in this.
I am developing facebok sample demo for get all friend list of login user.
I am running Friend Picker Sample successfully,But getstuck to extract all the details of friends like email,first_name,last_name etc.I am getting only id,name.
If anyone have idea please reply.
Here is mycode:-
public void getFriends(Properties properties, final OnFriendsRequestListener onFriendsRequestListener)
{
// if we are logged in
if (isLogin())
{
// move these params to method call parameters
Session session = getOpenSession();
Bundle bundle = null;
if (properties != null)
{
bundle = properties.getBundle();
}
Request request = new Request(session, "me/friends", bundle, HttpMethod.GET, new Request.Callback()
{
#Override
public void onCompleted(Response response)
{
List<GraphUser> graphUsers = typedListFromResponse(response, GraphUser.class);
FacebookRequestError error = response.getError();
if (error != null)
{
// log
logError("failed to get friends", error.getException());
// callback with 'exception'
if (onFriendsRequestListener != null)
{
onFriendsRequestListener.onException(error.getException());
}
}
else
{
// callback with 'complete'
if (onFriendsRequestListener != null)
{
List<Profile> friends = new ArrayList<Profile>(graphUsers.size());
for (GraphUser graphUser: graphUsers)
{
friends.add(Profile.create(graphUser));
}
onFriendsRequestListener.onComplete(friends);
}
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
// callback with 'thinking'
if (onFriendsRequestListener != null)
{
onFriendsRequestListener.onThinking();
}
}
else
{
String reason = Errors.getError(ErrorMsg.LOGIN);
logError(reason, null);
// callback with 'fail' due to not being loged
if (onFriendsRequestListener != null)
{
onFriendsRequestListener.onFail(reason);
}
}
}
Thanks in advance...
After very sturggle i try some methods and finally found solution.
public void getFriendsList() {
Bundle bundle=new Bundle();
bundle.putString("fields","id,name,first_name,last_name,email,picture,gender,birthday,work");
mAsyncRunner.request("me/friends",bundle,friendsRequsetListener,null);
}
RequestListener friendsRequsetListener =new RequestListener() {
#Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
String json = response;
try {
Log.e("Response","========>"+json);
}
catch(Exception e){
}
}
};
I hope this will help others. Thanks!!
I am working in Android app with Facebook integration but I am getting issue as whenever I try to logout the Facebook is not getting completely logout,
I searched a lot and finally I have used the code as follows:
public Facebook mFacebook = new Facebook(APP_ID);
SessionStore.clear(logoutActivity.this);
try {
mFacebook.logout(logoutActivity.this);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I tried the above code but not at all working,the Facebook is not getting complete logout. Could somebody help me out??
This May Will help to you...
Activity.mAsyncRunner.logout(this, new RequestListener() {
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
// User successfully Logged out
Toast.makeText(getApplicationContext(), "Successfully logged out ", Toast.LENGTH_LONG).show();
}
}
public void onIOException(IOException e, Object state) {
}
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
public void onFacebookError(FacebookError e, Object state) {
}
});
Example below url
http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
I am working on an android app in which i need to login with Facebook and fetch facebook friends.
I am using below code for fetching facebook friends:
public class FriendsRequestListener implements RequestListener {
/**
* Called when the request to get friends has been completed.
* Retrieve and parse and display the JSON stream.
*/
public void onComplete(final String response) {
try {
// process the response here: executed in background thread
Log.d("Facebook-Example-Friends Request", "response.length(): " +
response.length());
Log.d("Facebook-Example-Friends Request", "Response: " + response);
final JSONObject json = new JSONObject(response);
JSONArray d = json.getJSONArray("data");
int l = (d != null ? d.length() : 0);
Log.d("Facebook-Example-Friends Request", "d.length(): " + l);
for (int i=0; i<l; i++) {
JSONObject o = d.getJSONObject(i);
String n = o.getString("name");
String id = o.getString("id");
}
// Only the original owner thread can touch its views
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
}
}
#Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
The way to call this class:
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/friends", new FriendsRequestListener());
The problem is that my code is not doing anything on call of above code.
you can use this api https://graph.facebook.com/v1.0/me/friends?fields=picture,id,username&limit=${limit}&access_token=${accessToken}
this is a v1.0 api which allows user to fetch all friends list
To get all your facebook friends list you need to get "taggable_friends"
GraphRequestAsyncTask r = GraphRequest.newGraphPathRequest(fbToken,
"/me/taggable_friends", new Callback() {
#Override
public void onCompleted(GraphResponse response) {
//here you can parse the response
}
}
fbToken is your token from login. However, facebook uses paging, so you need to get the next page and then next value from friends request so you can parse it.
response.getJSONObject("paging").getString("next");
to get next page users using StringRequest
StringRequest sr = new StringRequest(Request.Method.GET, next,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
JSONObject friends = null;
try {
friends = new JSONObject(response);
parseResponse(friends);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
Check the full code Facebook friends list
I have got all the facebook albums (and their id )using facebook android sdk, and displayed them in a list.
Now on particular list item click, i want to get all the photos of that album (w.r.t "id" )
I got album id this way:
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "user_photos" },
new DialogListener() {
public void onComplete(Bundle values) {
mAsyncRunner.request("me/albums", new albumsRequestListener());
}
public void onFacebookError(FacebookError error) {
}
public void onError(DialogError e) {
}
public void onCancel() {
}
});
}
public class albumsRequestListener implements RequestListener {
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
try {
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
String count = "0";
JSONArray jArrData = new JSONArray();
jArrData = json.getJSONArray("data");
for (int i = 0; i < jArrData.length(); i++) {
JSONObject objAlbum = jArrData.getJSONObject(i);
String albumId = objAlbum
.getString("id");
String albumName = objAlbum
.getString("name");
if(objAlbum.has("count"))
{
count = objAlbum.getString("count");
}
else
{
count = "0";
}
FbAlbums obj = new FbAlbums();
obj.setId(albumId);
obj.setName(albumName);
obj.setCount(count);
listFbAlbums.add(obj);
}
FB.this.runOnUiThread(new Runnable() {
public void run() {
adapter = new FbAlbumsListAdapter(
getApplicationContext(),listFbAlbums);
lvAlmbums.setAdapter(adapter);
pb.setVisibility(View.INVISIBLE);
lvAlmbums.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), position, Toast.LENGTH_SHORT).show();
}
});
}
});
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
// TODO Auto-generated method stub
}
public void onMalformedURLException(MalformedURLException e,
Object state) {
// TODO Auto-generated method stub
}
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
How can i get the json that contains source of each album's images?
Or is there some other way?
Any help will be appreciated,
Thanks
Just use the album ID and add /photos to the end:
mAsyncRunner.request(albumid+"/photos", new photosRequestListener());