我希望我的应用程序只显示Facebook页面发布的帖子,而无需任何身份验证.这是代码:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String APP_ID = "1234567890";
String APP_SECRET = "1a2b3c4defghijk";
String OWNER_OF_FEED = "somepage";
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(
"https://graph.facebook.com/oauth/access_token?client_id="+
APP_ID +
"&client_secret="+APP_SECRET+"&grant_type=client_credentials");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String access_token = client.execute(get, responseHandler);
String uri = "https://graph.facebook.com/" + OWNER_OF_FEED + "/feed?"
+ access_token;
get = new HttpGet(uri);
String responseBody = client.execute(get, responseHandler);
// responseBody contains JSON-encoded feed
//textview.setText(responseBody);
TextView txtv = (TextView) findViewById(R.id.feed);
txtv.setText(String.valueOf(responseBody));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法在应用程序上获得任何内容.我已在清单文件中包含了Internet访问权限.
新代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
URL url;
try {
url = new URL("https://www.facebook.com/feeds/page.php?format=rss20&id=289781571139242");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*het 17
实际上有一种方法可以在没有用户许可的情况下获取页面公共源!
解
获取页面Facebook Profile Id:最简单的方法是查询Graph API http://graph.facebook.com/ {YourPageUsername}.例如对于oStream Android App https://graph.facebook.com/oStreamApp将返回Facebook Profile Id 289781571139242
请求Facebook页面订阅源的RSS:向https://www.facebook.com/feeds/page.php?format=rss20&id= {yourAppId} 发出http请求,例如对于oStream Android App,公共https:// www. facebook.com/feeds/page.php?format=rss20&id=289781571139242
一个Android的实施将仅仅是:
旁注 这个问题并不是关于Android,而只是关于Facebook API.
小智 5
我已经使用访问令牌完成了它.在Facebook开发人员"工具/访问令牌工具"中有一个工具你将获得一个名为"app token"的访问令牌,你可以这样使用:https: //graph.facebook.com/ "你的Facebook ID"/feed? access_token = "您的应用令牌" ;
然后你将获得你的feed的json对象,你将能够使用JSON解析方法做任何你想做的事情.
在有人更改密码或尝试在Facebook的应用管理页面中获取另一个"app secret id"之前,您的应用令牌不会移动.请享用!
| 归档时间: |
|
| 查看次数: |
7502 次 |
| 最近记录: |