John Watson
John Watson

Reputation: 869

invalid application id

I am using following code to load an image from android app to facebook. I have installed facebook sdk for android. But the app is not doing the intended. I am getting "invalid application id" in the logcat. What mistake am I making ?

Button mButton=(Button)findViewById(R.id.button);

mButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             Facebook mFacebook=new Facebook(yourAppID)

    byte[] data = null;
    Bitmap bi = BitmapFactory.decodeFile(imageLink);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
    data = baos.toByteArray();

    Bundle params = new Bundle();
    params.putString("method", "photos.upload");
    params.putByteArray("picture", data);

    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    mAsyncRunner.request(null, params, "POST",
            new SampleUploadListener(), null);
         }
     });







public class SampleUploadListener extends BaseRequestListener {

    @SuppressWarnings("unused")
    public void onComplete(final String response, final Object state) {
        try {
            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            String src = json.getString("src");

            PublishImage.this.runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Successfully Uploaded", 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 abstract class BaseRequestListener implements RequestListener {

    public void onFacebookError(FacebookError e, final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

    public void onFileNotFoundException(FileNotFoundException e,
            final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

    public void onIOException(IOException e, final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

    public void onMalformedURLException(MalformedURLException e,
            final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

}

Logcat

06-15 20:00:55.398: W/KeyCharacterMap(629): No keyboard for id 0
06-15 20:00:55.398: W/KeyCharacterMap(629): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-15 20:01:02.438: D/dalvikvm(629): GC_EXTERNAL_ALLOC freed 1214K, 59% free 2755K/6599K, external 1995K/2266K, paused 630ms
06-15 20:01:08.998: D/MediaPlayer(629): getMetadata
06-15 20:01:15.529: D/Facebook-Example(629): Response: {"error_code":101,"error_msg":"Invalid application ID.","request_args":[{"key":"method","value":"photos.upload"},{"key":"format","value":"json"}]}
06-15 20:01:15.529: W/Facebook-Example(629): Facebook Error: Invalid application ID.

EDIT: This worked

class ButtonListener1 implements View.OnClickListener{

    Facebook facebook=new Facebook(ID);

    @Override
    public void onClick(View v) {

        facebook.authorize(Pic.this, new String[] { "publish_stream" },
                new DialogListener() {

            @Override
            public void onFacebookError(FacebookError e) {
            // TODO Auto-generated method stub
            }

            @Override
            public void onError(DialogError dialogError) {
            // TODO Auto-generated method stub
            }

            @Override
            public void onComplete(Bundle values) {
                postToWall(values.getString(Facebook.TOKEN));                                              
            }

            private  void postToWall(String accessToken) {    


                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] data = baos.toByteArray();
                Bundle bundle = new Bundle();

                bundle.putString(Facebook.TOKEN, accessToken);
                bundle.putByteArray("facebookPictureData", data);
                // The byte array is the data of a picture.
                bundle.putByteArray("picture", getIntent().getExtras().getByteArray("data"));

                try {
                    facebook.request("me/photos", bundle, "POST");

                } catch (FileNotFoundException fileNotFoundException) {
                   // makeToast(fileNotFoundException.getMessage());
                } catch (MalformedURLException malformedURLException) {
                   // makeToast(malformedURLException.getMessage());
                } catch (IOException ioException) {
                   // makeToast(ioException.getMessage());
                }
            }


            @Override
            public void onCancel() {
            // TODO Auto-generated method stub
            }
        });



     }
}

Upvotes: 0

Views: 3878

Answers (1)

doles
doles

Reputation: 556

UPDATE: Here is the link to the Facebook.java class on facebook api reference website: http://developers.facebook.com/docs/reference/androidsdk/authentication/. Scroll down a few lines to find the authorize method. Are you sure you are using the com.facebook.android.Facebook class from the android sdk and not a class that you might have spun on your own?

The basic auth flow on android apps is as follows: 1. Make an instance of the Facebook class (that you have). 2. Make a call to Facebook.authorize() with the right permissions you need. 3. In the DialogListener call back of the authorize method, you can save the access_token and expiration time for future use. 4. In the activity that you called authorize from, override the "onActivityResult method and if that method is called with Facebook's result code (Facebook.DEFAULT_AUTH_ACTIVITY_CODE), then call Facebook.authorizeCallback with the received intent. 5. After these 4 steps are done, you are free to make graph api requests with Facebook.request() or AsyncFacebookRunner.request(). In these request methods, the first parameter is the graph path you want. So, for example, if you called the request method with path "me", you would get back profile information of the logged in user. END UPDATE-------

For starters, I think you necessarily need to have the graph path as the first parameter in your code on this line:

mAsyncRunner.request("*********7618/photos", params, "POST",
new SampleUploadListener(), null);
}
});

In my code it is the albumid/photos. Additionally, you need permissions from the user to upload to an album belonging to her. For that, you need to retrieve an access token using Facebook.authorize().

I am not sure I understand why you are getting the error related to the app id being not valid, however, from what I know this are the basic steps you need to do in order to post photos before you post photos.

I am sure someone will post a better answer soon, hope this helps in the meantime.

Upvotes: 1

Related Questions