问题描述
我使用Facebook的SDK的Android作为外部library.I我能够通过facebook.But登录时,我想分享图片和应用还没有在直通登录Facebook,我试图登录,但在这种情况下,我越来越异常,并获得访问令牌作为null.Please帮助
W / com.facebook.Session(15670):应不通过读取权限(电子邮件)的请求发布或授权管理
W / com.facebook.Session(15670):应不通过读取权限(read_friendlists)向请求发布或管理授权
请找到code:
的String [] =权限{电子邮件,
user_birthday
publish_stream
user_checkins
friends_checkins
read_friendlists
publish_checkins
publish_actions};
最终静态INT AUTHORIZE_ACTIVITY_RESULT_ code = 0;
Facebook的=新的Facebook(APP_ID);
mAsyncRunner =新AsyncFacebookRunner(脸谱);
facebook.authorize(getActivity(),权限,AUTHORIZE_ACTIVITY_RESULT_ code,
新DialogListener(){
公共无效的onComplete(束值){
通信System.err.println(内部的onComplete);
// getProfileInformation();
}
公共无效onFacebookError(FacebookError错误){
通信System.err.println(error.toString()+Facebook.authorize错误:); 尝试{
dialog.dismiss();
}赶上(例外五){
// TODO:处理异常
}
对话框= NULL;
}
公共无效onerror的(DialogError E){
通信System.err.println(e.toString()+Facebook.authorize DialogError:);
Log.d(this.getClass()。的getName(),
Facebook.authorize DialogError:
+ e.toString());
尝试{
dialog.dismiss();
}赶上(例外E1){
// TODO:处理异常
}
对话框= NULL;
}
公共无效onCancel(){
通信System.err.println(脸谱授权取消);
Log.d(this.getClass()。的getName(),
Facebook的授权取消);
尝试{
dialog.dismiss();
}赶上(例外五){
// TODO:处理异常
}
对话框= NULL;
}
}); } }
});
首先下载这个 Facebbok SDK,并包括在您的项目库项目。
在你的活动,在那里你要共享的图像:
私有静态位图B:
私有静态Facebook的mFacebook;
私有静态AsyncFacebookRunner mAsyncRunner;
的String [] =权限{photo_upload};
最终静态INT AUTHORIZE_ACTIVITY_RESULT_ code = 0;
在的onCreate()初始化这个变量:
如果(Constant.FACEBOOK_APP_ID == NULL)
{
Util.showAlert(这一点,警告,Facebook的一个应用ID必须是
+运行这个例子之前规定:见FbAPIs.java);
返回;
}mHandler =新的处理程序();
mFacebook =新的Facebook(Constant.FACEBOOK_APP_ID);
mAsyncRunner =新AsyncFacebookRunner(mFacebook);
SessionStore.restore(mFacebook,这一点);
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
开关(要求code)
{
案例AUTHORIZE_ACTIVITY_RESULT_ code:
{
mFacebook.authorizeCallback(要求code,结果code,数据);
打破;
}
}
}
onButtonClick从那里你要分享图片:
如果(!mFacebook.isSessionValid())
{
mFacebook.authorize(TabNoteActivity.this,权限,AUTHORIZE_ACTIVITY_RESULT_ code,新LoginDialogListener());
}
其他
{
捆绑PARAMS =新包();
ByteArrayOutputStream流=新ByteArrayOutputStream();
Utils.b.com preSS(Bitmap.Com pressFormat.PNG,100,流); //我在这里宣布使用方法JPG图像转换成位图放在utils类
字节[]的字节数组= stream.toByteArray();
params.putString(Facebook.TOKEN,mFacebook.getAccessToken());
params.putString(信息,Facebook应用测试);
params.putByteArray(图片报,字节);
mAsyncRunner.request(ME /照片,则params,POST,新PhotoUploadListener(),NULL);
}
公共类PhotoUploadListener扩展BaseRequestListener
{
// @覆盖
公共无效的onComplete(最后字符串的响应,最终对象状态)
{ mHandler.post(新的Runnable()
{
// @覆盖
公共无效的run()
{
Toast.makeText(Activity.this,图像成功共享,Toast.LENGTH_LONG).show();
}
});
} 公共无效onFacebookError(FacebookError错误)
{
Toast.makeText(getApplicationContext(),脸谱错误:+ error.getMessage(),Toast.LENGTH_LONG).show();
}
}
I am using Facebook SDK for android as external library.I am able to login through facebook.But when I want to share an image and app has not logged in thru facebook, I am trying to login but in that case I am getting exception and getting access token as null.Please help
W/com.facebook.Session(15670): Should not pass a read permission (email) to a request for publish or manage authorization W/com.facebook.Session(15670): Should not pass a read permission (read_friendlists) to a request for publish or manage authorization
Please find the code:
String[] permissions = { "email",
"user_birthday",
"publish_stream",
"user_checkins",
"friends_checkins",
"read_friendlists",
"publish_checkins",
"publish_actions" };
final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
facebook.authorize(getActivity(), permissions, AUTHORIZE_ACTIVITY_RESULT_CODE,
new DialogListener() {
public void onComplete(Bundle values) {
System.err.println("Inside oncomplete");
//getProfileInformation();
}
public void onFacebookError(FacebookError error) {
System.err.println(error.toString()+ "Facebook.authorize Error: ");
try {
dialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
}
dialog=null;
}
public void onError(DialogError e) {
System.err.println(e.toString()+ "Facebook.authorize DialogError: ");
Log.d(this.getClass().getName(),
"Facebook.authorize DialogError: "
+ e.toString());
try {
dialog.dismiss();
} catch (Exception e1) {
// TODO: handle exception
}
dialog=null;
}
public void onCancel() {
System.err.println("Facebook authorization canceled");
Log.d(this.getClass().getName(),
"Facebook authorization canceled");
try {
dialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
}
dialog=null;
}
});
}
}
});
First Download this Facebbok SDK and include it in your project as library project.
In your activity,where you want to share the image ::
private static Bitmap b;
private static Facebook mFacebook;
private static AsyncFacebookRunner mAsyncRunner;
String[] permissions = {"photo_upload" };
final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;
In onCreate() initialize this variables ::
if (Constant.FACEBOOK_APP_ID == null)
{
Util.showAlert(this, "Warning", "Facebook Applicaton ID must be "
+ "specified before running this example: see FbAPIs.java");
return;
}
mHandler=new Handler();
mFacebook = new Facebook(Constant.FACEBOOK_APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionStore.restore(mFacebook, this);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case AUTHORIZE_ACTIVITY_RESULT_CODE:
{
mFacebook.authorizeCallback(requestCode, resultCode, data);
break;
}
}
}
onButtonClick from where you are going to share a Image ::
if (!mFacebook.isSessionValid())
{
mFacebook.authorize(TabNoteActivity.this, permissions, AUTHORIZE_ACTIVITY_RESULT_CODE, new LoginDialogListener());
}
else
{
Bundle params = new Bundle();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Utils.b.compress(Bitmap.CompressFormat.PNG, 100, stream);//Here I am convert the JPG image into bitmap using declaring method in the Utils class
byte[] byteArray = stream.toByteArray();
params.putString (Facebook.TOKEN, mFacebook.getAccessToken());
params.putString ("message"," Facebook App Testing");
params.putByteArray("picture",byteArray);
mAsyncRunner.request("me/photos", params,"POST", new PhotoUploadListener(),null);
}
public class PhotoUploadListener extends BaseRequestListener
{
//@Override
public void onComplete(final String response, final Object state)
{
mHandler.post(new Runnable()
{
// @Override
public void run()
{
Toast.makeText(Activity.this,"Image has been shared Successfully",Toast.LENGTH_LONG).show();
}
});
}
public void onFacebookError(FacebookError error)
{
Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),Toast.LENGTH_LONG).show();
}
}
这篇关于Facebook的SDK在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!