3
好吧,我已經想出了這一個,但我想把它放在那裏,以防其他人遇到問題。基本上,我需要做的是發佈爲Facebook用戶擁有的頁面(即,我是John Doe,我是頁面Rum Ham的管理員;我想發佈到Rum Ham頁面)。Facebook SDK 4.x Android發佈爲Facebook頁面
好吧,我已經想出了這一個,但我想把它放在那裏,以防其他人遇到問題。基本上,我需要做的是發佈爲Facebook用戶擁有的頁面(即,我是John Doe,我是頁面Rum Ham的管理員;我想發佈到Rum Ham頁面)。Facebook SDK 4.x Android發佈爲Facebook頁面
所以,基本上,答案看起來像這樣
首先,你需要用這條線來記錄用戶在
LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions", "manage_pages", "publish_pages"));
然後,你需要獲取我們想要的頁面的訪問令牌發佈到
Bundle params = new Bundle();
//ok so access token here is "app_ID|app_secret"
params.putString("access_token", accessToken);
params.putString("fields", "id");
GraphRequest request = new GraphRequest(null, "me", params, HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e("Error", error.getErrorMessage());
} else {
JSONObject values = response.getJSONObject();
try {
//so I have to get the user ID first
String id = values.getString("id");
Bundle p = new Bundle();
p.putString("access_token", accessToken);
//yay nest the graph requests
//once we get the id we can get their pages
GraphRequest pagesRequest = new GraphRequest(null, "/" + id + "/accounts", p, HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e("Error", error.getErrorMessage());
} else {
//ok so here, we're getting the pages back in a few JSON wrappers
JSONObject values = response.getJSONObject();
JSONArray array = null;
try {
array = values.getJSONArray("data");
//ok, so here we're just iterating through the pages a user has, obviously you can handle this accordingly..
for (int i = 0; i < array.length(); i++) {
//ok, here's how to actually get the token
String access_token = array.getJSONObject(i).getString("access_token")
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
GraphRequest.executeAndWait(pagesRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
GraphRequest.executeAndWait(request);
}
好了,一旦我們得到的訪問令牌頁,這裏正是真正的滾泥柯瑞進場,Facebook的拒絕在他們的參考頁面上告訴你。 因此,忘記你已經閱讀過的任何關於需要提交你的應用程序與他們審查的任何內容。我所要做的就是創建一個新的訪問令牌
//create a new access token, facebook refers to this as a page access token
AccessToken token = new AccessToken("page_access_token", AccessToken.getCurrentAccessToken().getUserId(), Arrays.asList("publish_actions", "manage_pages", "publish_pages"), null, AccessTokenSource.FACEBOOK_APPLICATION_SERVICE,
AccessToken.getCurrentAccessToken().getExpires(), AccessToken.getCurrentAccessToken().getLastRefresh());
//then we simply update our current access token
AccessToken.setCurrentAccessToken(token);
現在,我們還沒有完成。最後,我們需要實際進行API調用才能創建帖子:
Bundle params = new Bundle();
params.putString("message", "Contents of message");
//here, token is our newly created AccessToken object
GraphRequest request = new GraphRequest(token, "/pageid/feed", params, HttpMethod.POST, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e("Error", error.getErrorMessage());
} else {
//do your own processing here for success
}
}
});
GraphRequest.executeAndWait(request);
}
}
而這就好比它。希望這可以幫助別人!