前言
原本在開發者沙盒(SandBox)模式下一切都還滿順利的,
不料上到Google Play發現原本的功能不work,
花了一些時間才發現小細節.
自己記錄一下, 同時也給需要的人參考一下.
以下我就自己的開發歷程,
先從沙盒模式說起,
最後再談關閉沙盒模式要注意的地方.
下載&Import FaceBook SDK
I'll skip the installation of Facebook in emulator here.
> Right click / 'File' on top
> Existing Android Code Into Workspace
> make sure the 'facebook' project is checked
如果Project有Error可以先檢查android-support-v4.jar的版本是否一致
最快的方法就是把最新的jar在project list裡面copy起來,
然後把其他project有用到的都先delete掉, 再paste上去.
環境設定
接下來看看我們的project要做哪些設定
1. library
2. Manifest設定
3. res/values/strings.xml加上
Facebook上建立應用程式後, copy App ID
4. 在Facebook App設定頁面加上Hash Key
Mac User
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
程式 - Facebook分享
// facebook
private static final String PERMISSION = "publish_actions";
private UiLifecycleHelper uiHelper;
private boolean canPresentShareDialog;
private PendingAction pendingAction = PendingAction.NONE;
private enum PendingAction {
NONE,
POST_PHOTO,
POST_STATUS_UPDATE
}
// Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback()
{
// callback when session changes state
@Override
public void call(final Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
// make request to the /me API
Request request = Request.newMeRequest(session, new Request.GraphUserCallback()
{
@Override
public void onCompleted(GraphUser user, Response response)
{
// If the response is successful
Log.d("facebook", "GraphUserCallback" + user.getId()+" " + response.toString());
if (session == Session.getActiveSession())
{
if (user != null)
{
performPublish(PendingAction.POST_STATUS_UPDATE, canPresentShareDialog);
}
}
if (response.getError() != null)
{
// Handle errors, will do so later.
}
}
});
request.executeAsync();
}
}
});
private void performPublish(PendingAction action, boolean allowNoSession) {
Session session = Session.getActiveSession();
if (session != null) {
pendingAction = action;
if (hasPublishPermission()) {
// We can do the action right away.
handlePendingAction();
return;
} else if (session.isOpened()) {
// We need to get new permissions, then complete the action when we get called back.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSION));
return;
}
}
if (allowNoSession) {
pendingAction = action;
handlePendingAction();
}
}
@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
PendingAction previouslyPendingAction = pendingAction;
// These actions may re-set pendingAction if they are still pending, but we assume they
// will succeed.
pendingAction = PendingAction.NONE;
switch (previouslyPendingAction) {
case POST_PHOTO:
//postPhoto();
break;
case POST_STATUS_UPDATE:
postStatusUpdate();
break;
}
}
private FacebookDialog.ShareDialogBuilder createShareDialogBuilder() {
return new FacebookDialog.ShareDialogBuilder(this)
.setName("fb app name")
.setDescription("app description")
.setLink("site link");
}
private void postStatusUpdate() {
if (canPresentShareDialog) {
FacebookDialog shareDialog = createShareDialogBuilder().build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else if (hasPublishPermission()) {
Bundle params = new Bundle();
params.putString("name", " link主標題 ");
params.putString("caption", " link副標題 ");
params.putString("message", " 描述 ");
params.putString("link", " 分享連結 ");
params.putString("picture", " 圖片url ");
Request request = new Request(Session.getActiveSession(), "me/feed", params, HttpMethod.POST);
request.setCallback(new Request.Callback() {
@Override
public void onCompleted(Response response) {
showPublishResult(null, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
private void showPublishResult(String message, GraphObject result, FacebookRequestError error) {
String title = null;
String alertMessage = null;
if (error == null) {
title = getString(R.string.success);
alertMessage = getString(R.string.successfully_posted_post);
} else {
title = getString(R.string.error);
alertMessage = error.getErrorMessage();
}
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(alertMessage)
.setPositiveButton(R.string.ok, null)
.show();
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
// fb 登入結果
Session.getActiveSession().onActivityResult(this, requestCode, responseCode, intent);
}
正式版本
1. Facebook APP設定頁面一定要把SandBox mode關閉
2. 確認Android app package name
> Export Signed Application Package
> KeyStore path別忘記副檔名.keystore !
> 輸入key的資訊 (一個keystore可以有很多把keys), Alias就是key的名稱
3. 複製正式版本的hashed key (建議在keystore的目錄下)
keytool -exportcert -alias (key的名稱) -keystore (keystore名稱).keystore | openssl sha1 -binary | openssl base64
這樣submit的正式版app就能夠正常分享資訊到Facebook啦!