Parseを使って、Facebookでログインをやってみる

# 事前準備 #
### ParseのSDKをダウンロード ###
* https://parse.com/docs/downloads から、最新Android SDKをダウンロードして、解凍する
* その後は、libsに保存に、以下のファイルを保存する
 * Parse-x.x.x.jar
 * ParseFacebookUtilsVX-x.x.x.jar

### facebook SDKを配置 ###
* build.gradleに入って、ビルドしてOK
* 方法は、後に記述する

### facebookのApp Id ###
* 例：　https://developers.facebook.com/apps/XXX/dashboard/

### ParseのApplicationIDとClient Key ###
* 例： https://www.parse.com/apps/XXX/edit#keys


# コード修正 #
### build.gradle ###
```
repositories {
    mavenCentral()
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ......
    compile files('libs/Parse-x.x.x.jar')
    compile files('libs/ParseFacebookUtilsVX-x.x.x.jar')
    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
}
```


### Application ###
```
package com.example.alinwi.facebooklogintest;

import android.app.Application;

import com.parse.Parse;
import com.parse.ParseFacebookUtils;

/**
 * Created by alinwi on 2015/06/04.
 */
public class LoginApplication extends Application {
    @Override
    public void onCreate() {
        /** Called when the Application-class is first created. */
        super.onCreate();
        //Parseの初期化
        Parse.initialize(this, ApplicationID, ClientKey);
        //Twitterを使う場合
//        ParseTwitterUtils.initialize("TwitterのConsumer key","TwitterのConsumer secret");
        //Facebookを使う場合
        ParseFacebookUtils.initialize(getApplicationContext());
    }
}
```

### AndroidManifest.xml ###
* Applicationの追加(LoginApplication)
* FacebookActivityの追加
* facebook_app_idの追加

```
<?xml version="1.0" encoding="utf-8"?>
<manifest .....

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        ......
        android:name=".LoginApplication">

        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

        <activity
            android:name=".MainActivity"
            ......
        </activity>

        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name" />
    </application>

</manifest>
```

### MainActivity ###

```
    protected void onCreate(Bundle savedInstanceState) {
        ......

        List<String> permissions = Arrays.asList("user_birthday", "user_location", "user_friends", "email", "public_profile");
        ParseFacebookUtils.logInWithReadPermissionsInBackground(LoginActivity.this, permissions, new LogInCallback() {
            @Override
            public void done(ParseUser user, ParseException err) {
                if (user == null) {
                    Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
                } else if (user.isNew()) {
                    Log.d("MyApp", "User signed up and logged in through Facebook!");
                } else {
                    Log.d("MyApp", "User logged in through Facebook!");
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
    }
```


# 確認 #
* facebook loginボタンをクリックする
* facebook login画面が出て、ログインする
* https://www.parse.com/apps/XXX/collections#class/_User に、一つのuserが増えた



# 参照 #
* https://parse.com/docs/android/guide#users-facebook-users
* https://developers.facebook.com/docs/android/getting-started
* [permissions](http://stackoverflow.com/questions/30177950/using-parsefacebookutils-loginwithreadpermissionsinbackground-does-not-save-face)
* [parse sdk](http://stackoverflow.com/questions/16614177/how-to-add-parse-to-an-android-studio-project)
* [古い手順](http://qiita.com/masahide318/items/ccc279a77f2d9de2bcae)