问题描述
我是试图共享职位的Facebook。登录,发布许可工作finefor第一次。如果我删除Facebook的网页应用程序它不要求我重新安装登录和后以您的名义发布许可屏幕下方是我的code,请帮我做了什么mistike。我试过很多,但不能正常工作。谢谢各位的意见,所以我可以在Android的改善不少。下面是我的code ...
公共类FacebokView延伸活动{
静态最后弦乐的applicationID =将String.valueOf(R.string.app_id);
会议会议;
私有静态最后的名单,其中,字符串> PERMISSIONS = Arrays.asList(publish_actions);
私有静态最后弦乐PENDING_PUBLISH_KEY =pendingPublishReauthorization;
私人布尔pendingPublishReauthorization = FALSE;
私人UiLifecycleHelper uiHelper;
//私人StatusCallback statusCallback;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.faceok_view);
//创建会话
this.session =的createSession();
Log.i(SESSION1 - >中,会议+ session.isOpened());
//开始的Facebook登录
//Session.openActiveSession(this,真,新Session.StatusCallback(){
StatusCallback statusCallback =新StatusCallback(){
//回调时,会话状态变化
@覆盖
公共无效呼叫(会话的会话,SessionState会状态,例外的例外){//读取
onSessionStateChange(会话,状态除外);
Log.i(OPENED_TOKEN_UPDATED,state.equals(SessionState.OPENED_TOKEN_UPDATED)+&其中;<试验);
如果(session.isOpened()&安培;&安培; session.getPermissions()包含(publish_actions)){
Log.i(FB SESSION1:publish_actions否则如果,session.getPermissions()包含(publish_actions)+OPENED_TOKEN_UPDATED ----+ state.equals(SessionState.OPENED_TOKEN_UPDATED));
publishStory();
Log.i(会话2 - >中,会议+ session.isOpened());
}否则如果(session.isOpened()){
Log.i(会议3 - >中,会议+ session.isOpened());
Log.i(FB届会议2:publish_actions否则如果,session.getPermissions()包含(publish_actions)+OPENED_TOKEN_UPDATED ----+ state.equals(SessionState.OPENED_TOKEN_UPDATED));
名单<字符串>权限= session.getPermissions();
如果(!isSubsetOf(权限,权限)){
pendingPublishReauthorization =真;
Session.NewPermissionsRequest newPermissionsRequest =新Session.NewPermissionsRequest(FacebookView.this,权限);
session.requestNewPublishPermissions(newPermissionsRequest);
}
}否则,如果(会话= NULL和放大器;!&安培; session.getState()== SessionState.CLOSED_LOGIN_FAILED){
Log.i(FB会议,不开);
Log.i(session4 - >中,会议+ session.isOpened());
完();
}
}
};
如果(session.getState()。等于(SessionState.CREATED_TOKEN_LOADED)){
Log.i(session5 - >中,会议+ session.isOpened());
session.openForRead(新Session.OpenRequest(本).setCallback(statusCallback));
Log.i(CREATED_TOKEN_LOADED,会话+ session.isOpened());
}否则如果(session.isOpened()及!&安培;!session.isClosed()){
Log.i(届会议6 - >中,会议+ session.isOpened());
OpenRequest REQ =新Session.OpenRequest(本);
req.setCallback(statusCallback);
session.openForRead(REQ);
} 其他 {
Log.i(session7 - >中,会议+ session.isOpened());
Session.openActiveSession(这一点,真,statusCallback);
}
}
私人无效onSessionStateChange(会话的会话,SessionState会状态,例外的例外){
如果(state.isOpened()){
如果(pendingPublishReauthorization&安培;&安培; state.equals(SessionState.OPENED_TOKEN_UPDATED)){
Log.i(onSessionStateChange ---> OPENED_TOKEN_UPDATED - >中,state.equals(SessionState.OPENED_TOKEN_UPDATED)+pendingPublishReauthorization - >中+ pendingPublishReauthorization);
pendingPublishReauthorization = FALSE;
publishStory();
Log.i(session12 - >中,会议+ session.isOpened());
}
}否则如果(state.isClosed()){
Log.i(session8 - >中,会议+ session.isOpened());
// shareButton.setVisibility(View.INVISIBLE);
}
}
保护无效publishStory(){
会话会话= Session.getActiveSession();
如果(会话!= NULL){
//检查发布权限
名单<字符串>权限= session.getPermissions();
如果(!isSubsetOf(权限,权限)){
pendingPublishReauthorization =真;
Session.NewPermissionsRequest newPermissionsRequest =新Session.NewPermissionsRequest(本,权限);
session.requestNewPublishPermissions(newPermissionsRequest);
返回;
}
//登录后化妆请求/ ME API
捆绑参数= getIntent()getExtras()。
串facebookResponse = parameters.getString(facebookResponse);
Log.i(fbResp从服务器1 publish_actions:,session.getPermissions()包含(publish_actions)+* --- facebookResponse ---- *+ facebookResponse);
尝试 {
JSONObject的jsonObj =新的JSONObject(facebookResponse);
parameters.putString(信息,jsonObj.getString(MsgForFb));
parameters.putString(姓名,jsonObj.getString(NameForFb));
parameters.putString(链接,jsonObj.getString(LinkForFb));
parameters.putString(描述,jsonObj.getString(DescForFb));
parameters.putString(图片报,jsonObj.getString(PicForFb));
}赶上(JSONException E1){
e1.printStackTrace();
}
Request.Callback回调=新Request.Callback(){
公共无效onCompleted(响应响应){
如果(response.getGraphObject()!= NULL){
Log.i(FB回应 - ,response.toString());
JSONObject的graphResponse = response.getGraphObject()getInnerJSONObject()。
字符串postid = NULL;
尝试 {
postId = graphResponse.getString(ID);
}赶上(JSONException E){
e.printStackTrace();
Log.e(FacebookView:回调,JSON错误:+ e.getMessage());
}
FacebookRequestError错误= response.getError();
如果(错误!= NULL){
Toast.makeText(FacebookView.this.getApplicationContext(),error.getErrorMessage(),Toast.LENGTH_LONG).show();
} 其他 {
Log.i(FacebookView:Request.Callbak:postId =,+ postId);
}
}
}
};
请求请求=新的请求(会话,我/饲料,参数,HttpMethod.POST,回调);
RequestAsyncTask任务=新RequestAsyncTask(要求);
task.execute();
Request.executeMeRequestAsync(会话,新Request.GraphUserCallback(){
//后与用户对象图形API响应回调
@覆盖
公共无效onCompleted(GraphUser用户,响应响应){
如果(用户!= NULL){
完();
}
}
});
}
}
@覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
Session.getActiveSession()onActivityResult(这一点,要求code,因此code,数据)。
}
私人会议的createSession(){
会话activeSession = Session.getActiveSession();
如果(activeSession == NULL || activeSession.getState()。在isClosed()){
activeSession =新Session.Builder(本).setApplicationId(的applicationID).build();
Session.setActiveSession(activeSession);
}
返回activeSession;
}
私人布尔isSubsetOf(集<字符串>的子集,收集和LT;字符串>超){
对于(字符串字符串:子集){
如果(!superset.contains(串)){
返回false;
}
}
返回true;
}
}
如果用户删除从Facebook应用程序,您的应用程序就会知道的唯一方法是,如果你犯了一个图的要求,它失败。
下面是错误处理一些常见的策略 - HTTPS://developers.facebook。 COM /文档/参考/ API /错误/
您也应该看看的美味样品附带的SDK,有一个handleError的()方法SelectionFragment.java实现在上面的链接中列出的策略。
i am try to share the post in facebook. login, publish permission is working finefor the first time. if i delete the app from facebook web it is not asking me reinstalling login and "Post on your behalf" publish permission screen below is my code Please help what i made mistike. I tried lot but is not working properly.. Thank you for your kind advice and so i can improve lot in android. Below is my code...
public class FacebokView extends Activity {
static final String applicationId = String.valueOf(R.string.app_id);
Session session;
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private UiLifecycleHelper uiHelper;
//private StatusCallback statusCallback;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faceok_view);
// create the session
this.session = createSession();
Log.i("session1--->", "session"+ session.isOpened());
// start Facebook Login
//Session.openActiveSession(this, true, new Session.StatusCallback() {
StatusCallback statusCallback = new StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {//READ
onSessionStateChange(session, state, exception);
Log.i("OPENED_TOKEN_UPDATED", state.equals(SessionState.OPENED_TOKEN_UPDATED) + "<<test");
if (session.isOpened() && session.getPermissions().contains("publish_actions")) {
Log.i("fb session1: publish_actions else if", session.getPermissions().contains("publish_actions") +"OPENED_TOKEN_UPDATED ----"+state.equals(SessionState.OPENED_TOKEN_UPDATED));
publishStory();
Log.i("session2--->", "session"+ session.isOpened());
} else if (session.isOpened()) {
Log.i("session3--->", "session"+ session.isOpened());
Log.i("fb session2: publish_actions else if", session.getPermissions().contains("publish_actions") +"OPENED_TOKEN_UPDATED ----"+state.equals(SessionState.OPENED_TOKEN_UPDATED));
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(FacebookView.this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
}
} else if(session != null && session.getState() == SessionState.CLOSED_LOGIN_FAILED){
Log.i("fb session:", "not open");
Log.i("session4--->", "session"+ session.isOpened());
finish();
}
}
};
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)){
Log.i("session5--->", "session"+ session.isOpened());
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
Log.i("CREATED_TOKEN_LOADED", "session"+ session.isOpened());
}else if (!session.isOpened() && !session.isClosed()) {
Log.i("session6--->", "session"+ session.isOpened());
OpenRequest req = new Session.OpenRequest(this);
req.setCallback(statusCallback);
session.openForRead(req);
} else {
Log.i("session7--->", "session"+ session.isOpened());
Session.openActiveSession(this, true, statusCallback);
}
}
private void onSessionStateChange(Session session, SessionState state,Exception exception) {
if (state.isOpened()) {
if (pendingPublishReauthorization && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
Log.i("onSessionStateChange--->OPENED_TOKEN_UPDATED -->", state.equals(SessionState.OPENED_TOKEN_UPDATED) + "pendingPublishReauthorization-->" + pendingPublishReauthorization);
pendingPublishReauthorization = false;
publishStory();
Log.i("session12--->", "session"+ session.isOpened());
}
} else if (state.isClosed()) {
Log.i("session8--->", "session"+ session.isOpened());
// shareButton.setVisibility(View.INVISIBLE);
}
}
protected void publishStory() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
// after login make request to the /me API
Bundle parameters = getIntent().getExtras();
String facebookResponse = parameters.getString("facebookResponse");
Log.i("fbResp from server 1 publish_actions:", session.getPermissions().contains("publish_actions") + "*---facebookResponse----*"+ facebookResponse);
try {
JSONObject jsonObj = new JSONObject(facebookResponse);
parameters.putString("message", jsonObj.getString("MsgForFb"));
parameters.putString("name", jsonObj.getString("NameForFb"));
parameters.putString("link", jsonObj.getString("LinkForFb"));
parameters.putString("Description", jsonObj.getString("DescForFb"));
parameters.putString("picture", jsonObj.getString("PicForFb"));
} catch (JSONException e1) {
e1.printStackTrace();
}
Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
if(response.getGraphObject() != null) {
Log.i("fb Response - ", response.toString());
JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
e.printStackTrace();
Log.e("FacebookView: callBack:", "JSON error : " + e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(FacebookView.this.getApplicationContext(), error.getErrorMessage(), Toast.LENGTH_LONG).show();
} else {
Log.i("FacebookView: Request.Callbak: postId = ", " "+postId);
}
}
}
};
Request request = new Request(session, "me/feed", parameters, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
finish();
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
private Session createSession() {
Session activeSession = Session.getActiveSession();
if (activeSession == null || activeSession.getState().isClosed()) {
activeSession = new Session.Builder(this).setApplicationId(applicationId).build();
Session.setActiveSession(activeSession);
}
return activeSession;
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
}
If the user deletes the app from Facebook, the only way your app will know is if you make a graph request, and it fails.
Here are some common strategies for handling errors - https://developers.facebook.com/docs/reference/api/errors/
You should also have a look at the Scrumptious sample that ships with the SDK, there's a handleError() method in SelectionFragment.java that implements the strategies outlined in the link above.
这篇关于Facebook发布代表你不是工作在Android的Facebook的SDK 3.0.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!