Reputation: 396
I'm creating a game using Libgdx and now I've reached the part where the user can connect to his facebook account, this way the user can compare his scores with friends.
I was able to implement the facebook sdk package in my project, get an app ID on facebook and hashkey. After that i've hit a wall. I've tried all the available resources that i can find online but nothing is working (spent 2 days on it).
What i understood is that i need to create in my core project an Interface, which will be linked to my facebook authentication class in my android folder.
I've attempted several tries with many projects available online with no luck.
Any help or available tutorials will be much appreciated.
Upvotes: 0
Views: 424
Reputation: 98
I'm not sure what all you've tried already, but here's a good source:
https://github.com/TomGrill/gdx-facebook/wiki
From section 3 of the wiki linked above, note:
IMPORTANT facebook-android-sdk comes as .aar file. Some IDEs (f.e. Eclipse) are not capable of handling this file type. That means you have to start your app using the command line to make it work:
Linux/Max:
./gradlew android:installDebug android:run
Windows:
gradlew android:installDebug android:run
I'll try to get you started up on having the user log in and I bet you can take it from there. If you run into issues, then feel free to ask. Some parts are arbitrary and based on preference.
Before starting, a few things to keep in mind are:
Steps:
Configure the main gradle file
Directory: project_root/build.gradle
Just to clarify, the main gradle file is the Project Root -> build.gradle, not Project Root -> core -> build.gradle.
Add:
gdxFacebookVersion = '1.4.1'
Under:
allprojects {
...
ext {
...
// here
}
...
}
Add:
compile "de.tomgrill.gdxfacebook:gdx-facebook-android:$gdxFacebookVersion"
Under:
project(":android") {
...
dependencies {
...
// here
}
}
Add:
compile "de.tomgrill.gdxfacebook:gdx-facebook-core:$gdxFacebookVersion"
Under:
project(":core") {
...
dependencies {
...
// here
}
}
Note that one is ...:gdx-facebook-android:...
and the other is ...:gdx-facebook-core:...
.
Configure the Android gradle file
Directory: project_root/android/build.gradle
Ensure that the minSdkVersion
is not below 15, as required by the Facebook API. This will probably be set to 9 by default, if the official libGDX setup tool was used to create the project.
android {
...
defaultConfig {
...
minSdkVersion 15
...
}
...
}
This screenshot might answer another question, or two, which I feel one might have at this point. It's a libGDX project that successfully uses the GDXFacebook API; although, I changed the applicationId
for the occasion.
Store the app's Facebook App ID in an XML file
Directory: project_root/android/res/values/
Add a new XML file, which one could arbitrarily name "facebook.xml", or just use the "strings.xml" file that should already be there.
Now add the following to the XML file:
<string name="facebook_app_id">Your_Facebook_API_ID</string>
Example:
<string name="facebook_app_id">1559365201012064</string>
Add Internet permission and FacebookActivity to the AndroidManifest.xml
Directory: project_root/android/AndroidManifest.xml
Add:
<uses-permission android:name="android.permission.INTERNET" />
Under:
<manifest xmlns:android="htt ... >
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="20" />
// here
...
</manifest>
Add:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" />
<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" />
Under:
<manifest xmlns:android="htt ... >
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.INTERNET" />
...
<application
android:allowBackup="true" ... >
<activity
android:name=" ... >
...
</activity>
// here
</application>
</manifest>
Add helper class in the core source directory
Directory: project_root/core/src/[somewhere_around_here]
Anywhere in the above directory, add the following class (or your own helper class to get the same job done):
https://github.com/ronrihoo/GDX-Facebook-API-Helper/blob/master/FacebookApi.java
(Backup: https://www.snip2code.com/Snippet/2695867/Example-helper-class-for-GDXFacebook)
From one of your main classes, create an instance of FacebookApi (the arbitrary helper class), like so:
...
// TODO: not the best place to keep the app ID; change later
private static final String API_APP_ID = "Your_App_ID";
private FacebookApi facebookApi;
...
// and instantiate in a method (or constructor)
private void setupFbApiInst() {
facebookApi = new FacebookApi(API_APP_ID);
}
Test Facebook sign-in
Use the helper instance to invoke the signIn() method, which should then, on its own, handle the sign-in screen/activity for the user (you don't have to build this screen, since it's provided by the Facebook API):
...
facebookApi.signIn();
...
The last piece of code, above, might ideally go in a button's listener, like so:
fbSignInButton.addListener(new ClickListener() {
@Override
public void clicked() {
facebookApi.signIn();
}
});
In this case, when the button has been clicked, the Facebook sign-in activity should appear (the following cropped screenshot is from a tablet device).
At a basic level, that should be it for having the user sign in to Facebook.
The next steps
For the rest of your objectives, looking through the following example should help, as you modify and build on the arbitrary Facebook API helper class (or however you choose to build this feature).
You might have to tweak the app settings on the developers.facebook.com site.
The next thing to handle would probably be the Facebook permissions. The arbitrary helper class, FacebookApi.java, starts out with the basic permissions as suggested in the GDXFacebook wiki and Facebook API docs.
You'll have to add more permissions as needed. The GdxFacebookSampleApp.java class, linked above, is a great example for getting started on that.
This should work, as it does for me. You just might have to make modifications, as needed, and implement your own error handling to help guide you troubleshoot problems.
I anticipate some issues occurring in between these steps and, at this time, I can't comment yet; however, I can respond by editing the answer. So feel free to ask questions and I will react/respond as I become available.
Upvotes: 1