不一定要在 facebook 应用程序中心创建一个应用程序来在墙上(或等)发布消息。我也在创建一个支持 facebook 的游戏,它正在发布带有链接和标题的照片,但游戏没有针对同一问题添加到 facebook 应用中心。
首先,我在https://developers.facebook.com/apps上创建了一个应用程序,我们应该记住 AppId(API KEY),而不是为 Android 选项卡启用应用程序并输入哈希码。使用命令由您的密钥库生成哈希keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
码,其中“androiddebugkey”是您的密钥名称(用于导出),“~/.android/debug.keystore”是您的密钥库(用于导出 ofc)。
我的班级正在发布照片
public class EveryBodyActivity extends Activity {
public final String API_KEY = "YOUR API KEY HERE";
public final String[] permissions = {"publish_stream"};
Facebook facebook = new Facebook(API_KEY);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton imageButton = (ImageButton)findViewById(R.id.button);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
authorizeAndPostMassage();
}
});
}
public void authorizeAndPostMassage() {
facebook.authorize(this, permissions, Facebook.FORCE_DIALOG_AUTH, new DialogListener () {
@Override
public void onComplete(Bundle values) {
Toast.makeText(EveryBodyActivity.this, "Authorization successful", Toast.LENGTH_SHORT).show();
postMassage();
}
@Override
public void onFacebookError(FacebookError e) {
Toast.makeText(EveryBodyActivity.this, "Facebook error, try again later", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(DialogError e) {
Toast.makeText(EveryBodyActivity.this, "Error, try again later", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
//Этот коллбэк никогда не срабатывает, вероятно, ошибка в SDK
Toast.makeText(EveryBodyActivity.this, "Authorization canceled", Toast.LENGTH_SHORT).show();
}});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
private void postMassage() {
Bundle imageBundle = new Bundle();
imageBundle.putString(Facebook.TOKEN, facebook.getAccessToken());
imageBundle.putString("method", "photos.upload");
imageBundle.putString("caption", "New Voodoo Doll");
byte[] data = null;
Bitmap bi = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
imageBundle.putByteArray("picture", data);
Toast.makeText(EveryBodyActivity.this, "Sending VooDoo...", Toast.LENGTH_SHORT).show();
try {
facebook.request(null, imageBundle, "POST");
} catch (FileNotFoundException fileNotFoundException) {
Toast.makeText(getApplicationContext(), (fileNotFoundException.getMessage()), 100);
} catch (MalformedURLException malformedURLException) {
Toast.makeText(getApplicationContext(), (malformedURLException.getMessage()), 100);
} catch (IOException ioException) {
Toast.makeText(getApplicationContext(), (ioException.getMessage()), 100);
}
}