Facebook登录 AndroidStudio

时间:2023-03-09 01:52:25
Facebook登录 AndroidStudio


接入SDK-1
一、Add Facebook SDK to Your Project
To use Facebook SDK in a project, add it as a build dependency and import it. If you are starting a new project, follow all the steps below. To add Facebook SDK to an existing project, start with step 3.
1. Go to Android Studio | New Project | Minimum SDK
2. Select "API 15: Android 4.0.3" or higher and create your new project.
3. In your project, open your_app | Gradle Scripts | build.gradle
4. Add the Maven Central Repository to build.gradle before dependencies:
repositories {
        mavenCentral()
}
5. Add compile 'com.facebook.android:facebook-android-sdk:[4,5)' to your build.gradle dependencies.
6. Build your project.
7. Import Facebook SDK into your app and init it:
import com.facebook.FacebookSdk;
@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     FacebookSdk.sdkInitialize(getApplicationContext());// Initialize the SDK before executing any other operations, especially, if you're using Facebook UI elements.
}

二、Add Facebook App ID
Add your Facebook App ID to your app and update your Android manifest.
1. Open your strings.xml file, for example: /app/src/main/res/values/strings.xml.
2. Add a new string with the name facebook_app_id containing the value of your Facebook App ID:
<string name="facebook_app_id">1157270487625628</string>
3. Open AndroidManifest.xml.
4. Add a uses-permission element to the manifest:
<uses-permission android:name="android.permission.INTERNET"/>
5. Add a meta-data element to the application element:
<application android:label="@string/app_name" ...>
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
</application>

三、Tell us about your Android project
Package Name
Your package name uniquely identifies your Android app. We use this to let people download your app from Google Play if they don't have it installed. You can find this in your Android Manifest
com.dongbai.mm.xiu
Default Activity Class Name
This is the fully qualified class name of the activity that handles deep linking. We use this when we deep link into your app from the Facebook app. You can also find this in your Android Manifest
com.lokinfo.m95xiu.WelcomeActivity

四、Add your development and release key hashes
To ensure the authenticity准确 of the interactions互动 between your app and Facebook, you need to supply us with the Android key hash for your development environment. If your app has already been published, you should add your release key hash too.

You'll have a unique development key hash for each Android development environment. To generate a development key hash, on Mac, run the following command:
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

On Windows, run this command:
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64
Facebook登录 AndroidStudio
【4SrCEIfuM++ddKXQh34fqEsxXsI=】
This command will generate a 28-character key hash unique to your development environment. Copy and paste it into the field below. You will need to provide a development key hash for the development environment of each person who works on your app.
If your app has already been published, you should also add a hash of your release key.

Android apps must be digitally signed with a release key before you can upload them to the store. To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore:
keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64
This will generate a 28-character string that you should copy and paste into the field below. Also, see the Android documentation for signing your apps.

五、Track App Installs and App Opens
App Events let you measure installs on your mobile app ads, create high value audiences观众 for targeting, and view analytics including user demographics统计学. To automatically log app activation events, add the following code to the onCreate() method of your app's Application class:
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);  
Now, when people install or engage with your app, you'll see this data reflected in your app's Insights dashboard.

Next Steps
Congratulations! You have added the Facebook SDK to your project. You are now in the next stage in integrating your app with Facebook. What do you want to do next? Skip to Developer Dashboard or Documentation

接入SDK-2
Android Studio Setup
To use Facebook SDK in a project, add it as a build dependency and import it.
1. Go to Android Studio | New Project | Minimum SDK
2. Select API 15: Android 4.0.3 or higher and create your new project.
3. After you create a new project, open your_app | build.gradle
4. Add this to Module-level /app/build.gradle before dependencies:
repositories {
    mavenCentral() 
}
5. Add the compile dependency with the latest version of the Facebook SDK in the build.gradle file:
dependencies { 
  compile  characher string. Copy and paste this Release Key Hash into your Facebook App ID's Android settings.

You should also check that your Facebook App ID's Android setting also contain the correct package name and main activity class for your Android package.

Using the Facebook SDK with Maven
You can declare the Maven dependency依赖关系 with the latest最新 available version of the Android SDK:
<dependency>
  <groupId>com.facebook.android</groupId>
  <artifactId>facebook-android-sdk</artifactId>
  <version>PUT_LATEST_VERSION_HERE</version>
</dependency> 

Troubleshooting疑难解答 Sample Apps
If you have a problem running a sample app, it may be related与…有关 to the key hash. You may see one of the following scenarios:
  • A native Login Dialog appears but after accepting the permissions you are still in a logged out退出 state. The logcat also contains an exception: OrcaServiceQueue(504):com.facebook.orca.protocol.base.ApiException: remote_app_id does not match stored id
  • A non-native Login Dialog appears with an error message: ''..App is Misconfigured木有配置 for facebook login...''.
        Check your key hash and you can make sure you use the correct key hash. 
        You can also manually modify the sample code to use the right key hash. For example in HelloFacebookSampleActivity class make a temporary临时 change to the onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        PackageInfo info = getPackageManager().getPackageInfo("com.facebook.samples.hellofacebook", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (NameNotFoundException e) {
    } catch (NoSuchAlgorithmException e) {
    }

        Save your changes and re-run the sample. Check your logcat output for a message similar to this:
12-20 10:47:37.747: D/KeyHash:(936): 478uEnKQV+fMQT8Dy4AKvHkYibo=
        Save the key hash in your developer profile. Re-run the samples and verify that you can log in successfully