2016-06-24 4 views
0

私はアンドロイドのFacebook認証を使用してサインインを実装する必要があるプロジェクトに取り組んでいます。 Facebook APIを実装しましたが、トークンを取得するためにコードが必要なときにアクセストークンを提供します。 FB apiを使用してコードを取得するだけでは使用できないというアドバイスがありましたが、自分のログインフローをプログラムする必要があります。私はfbの開発者ページの基本的なドキュメントがあることを知っていますが、アンドロイドでこの機能を実装することについては何も言いません。AndroidでFacebook用のログインフローを手動で作成する

ご協力いただければ幸いです。

答えて

1

https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow

のAndroid SDK:https://developers.facebook.com/docs/android/

のAndroid SDKのソースコードを手動でログインフローを構築

。私が思ったように、それを行う最も簡単な方法はby using retrofit. - >あなたのアプリケーションにOAuthを統合することです。

コードスニペット:

// you should either define client id and secret as constants or in string resources 
private final String clientId = "xxxxxxxxxxxxxxxxx"; 
private final String responseType = "code"; 

/** 
* same as in manifest in intent filter 
*/ 
private final String redirectUri = "http://www.example.com/gizmos"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    try { 
     PackageInfo info = getPackageManager().getPackageInfo(
       "xxxxxxxxxxxxxxx", // replace with your unique package name 
       PackageManager.GET_SIGNATURES); 
     for (Signature signature : info.signatures) { 
      MessageDigest md = MessageDigest.getInstance("SHA"); 
      md.update(signature.toByteArray()); 
      Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT)); 
     } 
    } catch (PackageManager.NameNotFoundException e) { 

    } catch (NoSuchAlgorithmException e) { 

    } 

    Button loginButton = (Button) findViewById(R.id.loginbutton); 
    loginButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(
        Intent.ACTION_VIEW, 
        Uri.parse(ServiceGenerator.API_BASE_URL + "/dialog/oauth" + 
          "?client_id=" + clientId + 
          "&redirect_uri=" + redirectUri + 
          "&response_type=" + responseType)); 
      startActivity(intent); 
     } 
    }); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    // the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent 
    Uri uri = getIntent().getData(); 
    if (uri != null && uri.toString().startsWith(redirectUri)) { 
     // use the parameter your API exposes for the code (mostly it's "code") 
     String code = uri.getQueryParameter("code"); 
     if (code != null) { 
      Log.i("code", code); 
      // get access token 
      // we'll do that in a minute 
     } else if (uri.getQueryParameter("error") != null) { 
      // show an error message here 
     } 
    } 
} 

}

+0

ありがとう、男! –

1

必要なものはすべて公式のFacebookページにあります。私は答えを見つけたhttps://github.com/facebook/facebook-android-sdk

+0

私はすでにそのAPIを実装しましたが、(私が正しく理解していれば)手動ログインの流れは、それを使用しません書きました。だから、あなたはすでに私が見たリンクの束を私にくれました。私の質問は、このログインフローを実装するために何を使用しなければならないかです。 OAuthでの改修? FacebookのAPIを変更しますか?いくつかのokHttp? – Kamajabu

+0

@ Reiz3Nライブラリを使用する必要はありません(必要な場合は可能です)。それは単なるGETリクエストです。 http://stackoverflow.com/questions/3505930/make-an-http-request-with-androidを参照してください – Code

+0

@ Reiz3Nなぜ正式なSDKを使用しないでください? – Code

関連する問題