尝试通过自己的应用在Facebook上访问我的状态时遇到了问题。
当我尝试使用Request.newMeRequest(session, new Request.GraphUserCallback()
方法获取我的信息时,它运行良好。在这种情况下,要获取状态Feed,我使用new Request(session, "/v2.1/me/feed?fields=message", params, HttpMethod.GET, new Request.Callback()
方法。
但是,我收到了JSON反馈中的错误消息:
必须使用活动访问令牌来查询有关当前用户的信息。
以下是我的代码:
public class TimelineActivity extends Activity {
private String TAG = "TimelineActivity";
String[] arrayStatus;
ListView lvStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timeline);
lvStatus = (ListView) findViewById(R.id.lvStatus);
Session session = Session.getActiveSession();
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, Arrays.asList("public_profile", "user_status",
"read_stream"));
List<String> permision = new ArrayList<String>();
permision.add("user_status");
permision.add("read_stream");
Session.openActiveSession(this, true,permision, callback);
/*if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setPermissions(
Arrays.asList("public_profile", "user_status",
"read_stream")).setCallback(callback));
} else {
Session.openActiveSession(this, true, callback);
}*/
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Bundle params = new Bundle();
params.putString("access_token", session.getAccessToken());
/* make the API call */
new Request(session, "/v2.1/me/feed?fields=message", params,
HttpMethod.GET, new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
Log.e(TAG, response.getError().toString());
GraphObject graphObject = response
.getGraphObject();
if (graphObject != null) {
Log.d(TAG, "get data :success");
JSONObject jsonObject = graphObject
.getInnerJSONObject();
Log.d(TAG, jsonObject.toString());
try {
JSONArray array = jsonObject
.getJSONArray("data");
Log.d(TAG, array.toString());
for (int i = 0; i < array.length(); i++) {
JSONObject object = (JSONObject) array
.get(i);
Log.i(TAG,
"id = "
+ object.get("message"));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "JSON error");
}
} else {
Log.e(TAG, "failed to get data");
}
}
}).executeAsync();
}
}
};
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
}
我该怎么办?