すでにアプリのログイン手順がありましたが、Facebook グラフ API と統合しようとすると、FB でログインしようとすると、アプリが受け入れられません。ここで FB ボタンのコードを貼り付けています。
case R.id.fbButton:
authenticateOnFacebook();
sendFacebookIdToServer(APP_ID, getApplicationContext());
break;
Facebookの認証方法、これで私はgetcredentials方法も使用しています。
public void authenticateOnFacebook()
{
try{
String[] permissions={"user_birthday", "email"};
facebook.authorize(this.activity, permissions, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
}
@Override
public void onError(DialogError e) {
}
@Override
public void onComplete(Bundle values) {
try{
String facebookId = getCredentials((String) values.get("access_token"));
sendFacebookIdToServer(facebookId, context);
} catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onCancel() {
}
});
} catch(Exception e){
e.printStackTrace();
}
}
protected String getCredentials(String token) {
JSONObject jObject=null;
String facebookId = null;
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, token);
try {
jObject = new JSONObject(facebook.request("me", params));
}catch(Exception e){
e.printStackTrace();
}
if(jObject!=null)
{
try {
facebookId = jObject.getString("id");
}catch(Exception e){
e.printStackTrace();
}
}
return facebookId;
}
サーバーにリクエストを送信して認証するために、このメソッドを使用しています
public void sendFacebookIdToServer(String facebookId, Context context){
String accessToken = null;
DatabaseHelper helper = new DatabaseHelper(context);
DatabaseUtility dao = new DatabaseUtility(helper);
try {
accessToken = dao.getAccessToken();
} catch (Exception e1) {
e1.printStackTrace();
}
Map<String , String> params = new HashMap<String,String>();
params.put(Constants.FACEBOOK_ID, facebookId);
params.put(Constants.ACCESS_TOKEN_PARAM, accessToken);
Status status = null;
try {
status = Utils.addFacebookAccount(params, context);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
if((status != null) && (status.getStatus().equalsIgnoreCase(Constants.SUCCESS))){
navigateNowLisnScreenWithSuccess();
}
}
private void navigateNowLisnScreenWithSuccess() {
// TODO Auto-generated method stub
Intent i = new Intent(context,TabView.class);
startActivity(i);
}
ログキャット:
01-17 10:34:26.004: W/System.err(575): java.lang.IllegalArgumentException: Invalid context argument
01-17 10:34:26.004: W/System.err(575): at android.webkit.CookieSyncManager.createInstance(CookieSyncManager.java:92)
01-17 10:34:26.004: W/System.err(575): at com.facebook.android.Facebook.startDialogAuth(Facebook.java:308)
01-17 10:34:26.014: W/System.err(575): at com.facebook.android.Facebook.authorize(Facebook.java:207)
01-17 10:34:26.014: W/System.err(575): at com.lisnx.activity.LoginActivity.authenticateOnFacebook(LoginActivity.java:164)
01-17 10:34:26.014: W/System.err(575): at com.lisnx.activity.LoginActivity.onClick(LoginActivity.java:262)
01-17 10:34:26.014: W/System.err(575): at android.view.View.performClick(View.java:3480)
01-17 10:34:26.014: W/System.err(575): at android.view.View$PerformClick.run(View.java:13983)
01-17 10:34:26.014: W/System.err(575): at android.os.Handler.handleCallback(Handler.java:605)
01-17 10:34:26.014: W/System.err(575): at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 10:34:26.014: W/System.err(575): at android.os.Looper.loop(Looper.java:137)
01-17 10:34:26.014: W/System.err(575): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-17 10:34:26.014: W/System.err(575): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 10:34:26.024: W/System.err(575): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 10:34:26.024: W/System.err(575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-17 10:34:26.024: W/System.err(575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-17 10:34:26.024: W/System.err(575): at dalvik.system.NativeStart.main(Native Method)
01-17 10:34:26.045: W/System.err(575): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-17 10:34:26.045: W/System.err(575): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:434)
01-17 10:34:26.045: W/System.err(575): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
01-17 10:34:26.045: W/System.err(575): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
01-17 10:34:26.045: W/System.err(575): at com.lisnx.dao.TokenDao.getAccessToken(TokenDao.java:59)
01-17 10:34:26.054: W/System.err(575): at com.lisnx.dao.DatabaseUtility.getAccessToken(DatabaseUtility.java:111)
01-17 10:34:26.054: W/System.err(575): at com.lisnx.activity.LoginActivity.sendFacebookIdToServer(LoginActivity.java:223)
01-17 10:34:26.054: W/System.err(575): at com.lisnx.activity.LoginActivity.onClick(LoginActivity.java:263)
01-17 10:34:26.054: W/System.err(575): at android.view.View.performClick(View.java:3480)
01-17 10:34:26.054: W/System.err(575): at android.view.View$PerformClick.run(View.java:13983)
01-17 10:34:26.054: W/System.err(575): at android.os.Handler.handleCallback(Handler.java:605)
01-17 10:34:26.054: W/System.err(575): at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 10:34:26.054: W/System.err(575): at android.os.Looper.loop(Looper.java:137)
01-17 10:34:26.054: W/System.err(575): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-17 10:34:26.054: W/System.err(575): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 10:34:26.064: W/System.err(575): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 10:34:26.064: W/System.err(575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-17 10:34:26.064: W/System.err(575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-17 10:34:26.064: W/System.err(575): at dalvik.system.NativeStart.main(Native Method)
01-17 10:34:26.794: D/dalvikvm(575): GC_CONCURRENT freed 404K, 14% free 7244K/8327K, paused 5ms+4ms
01-17 10:34:27.254: D/StrictMode(77): StrictMode policy violation; ~duration=10 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=151 violation=2
01-17 10:34:27.254: D/StrictMode(77): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1074)
01-17 10:34:27.254: D/StrictMode(77): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:94)
01-17 10:34:27.254: D/StrictMode(77): at libcore.io.IoBridge.open(IoBridge.java:390)
01-17 10:34:27.254: D/StrictMode(77): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-17 10:34:27.254: D/StrictMode(77): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-17 10:34:27.254: D/StrictMode(77): at com.android.internal.os.AtomicFile.startWrite(AtomicFile.java:59)
01-17 10:34:27.254: D/StrictMode(77): at android.content.SyncStorageEngine.writeStatusLocked(SyncStorageEngine.java:1906)
01-17 10:34:27.254: D/StrictMode(77): at android.content.SyncStorageEngine.handleMessage(SyncStorageEngine.java:336)
01-17 10:34:27.254: D/StrictMode(77): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 10:34:27.254: D/StrictMode(77): at android.os.Looper.loop(Looper.java:137)
01-17 10:34:27.254: D/StrictMode(77): at com.android.server.ServerThread.run(SystemServer.java:744)
01-17 10:34:27.254: D/StrictMode(77): StrictMode policy violation; ~duration=6 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=151 violation=1
01-17 10:34:27.254: D/StrictMode(77): at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1048)
01-17 10:34:27.254: D/StrictMode(77): at libcore.io.BlockGuardOs.write(BlockGuardOs.java:178)
01-17 10:34:27.254: D/StrictMode(77): at libcore.io.IoBridge.write(IoBridge.java:447)
01-17 10:34:27.254: D/StrictMode(77): at java.io.FileOutputStream.write(FileOutputStream.java:187)
01-17 10:34:27.254: D/StrictMode(77): at java.io.OutputStream.write(OutputStream.java:82)
01-17 10:34:27.254: D/StrictMode(77): at android.content.SyncStorageEngine.writeStatusLocked(SyncStorageEngine.java:1915)
01-17 10:34:27.254: D/StrictMode(77): at android.content.SyncStorageEngine.handleMessage(SyncStorageEngine.java:336)
01-17 10:34:27.254: D/StrictMode(77): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 10:34:27.254: D/StrictMode(77): at android.os.Looper.loop(Looper.java:137)
01-17 10:34:27.254: D/StrictMode(77): at com.android.server.ServerThread.run(SystemServer.java:744)
01-17 10:34:27.264: D/StrictMode(77): StrictMode policy violation; ~duration=1 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=151 violation=1
01-17 10:34:27.264: D/StrictMode(77): at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1048)
01-17 10:34:27.264: D/StrictMode(77): at libcore.io.BlockGuardOs.fsync(BlockGuardOs.java:84)
01-17 10:34:27.264: D/StrictMode(77): at java.io.FileDescriptor.sync(FileDescriptor.java:71)
01-17 10:34:27.264: D/StrictMode(77): at android.os.FileUtils.sync(FileUtils.java:111)
01-17 10:34:27.264: D/StrictMode(77): at com.android.internal.os.AtomicFile.finishWrite(AtomicFile.java:80)
01-17 10:34:27.264: D/StrictMode(77): at android.content.SyncStorageEngine.writeStatusLocked(SyncStorageEngine.java:1918)
01-17 10:34:27.264: D/StrictMode(77): at android.content.SyncStorageEngine.handleMessage(SyncStorageEngine.java:336)
01-17 10:34:27.264: D/StrictMode(77): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 10:34:27.264: D/StrictMode(77): at android.os.Looper.loop(Looper.java:137)
01-17 10:34:27.264: D/StrictMode(77): at com.android.server.ServerThread.run(SystemServer.java:744)