Sign In with LinkedIn Tutorial
Introduction
Sign In with LinkedIn enables users to sign in to Apps using their LinkedIn accounts.
Prerequisites
To begin with this tutorial, you will need:
- An app created at Back4App.
- See the Create New App tutorial to learn how to create an app at Back4App.
- Set up a Subdomain for your Back4app app
- See Activating your Web Hosting and Live Query to learn how to create an subdomain in Back4App.
- An LinkedIn Developer account.
Step 1 - Create a New Back4App App
First of all, it’s necessary to make sure that you have an existing app created at Back4App. However, if you are a new user, you can check this tutorial to learn how to create one.
Step 2 - Create a new LinkedIn App
Log into your LinkedIn Developer account and click Create App
and choose OAuth client ID
Choose an App name and fill the required fields such as Business email
and App logo
. Agree to the therms and click Create app
In your newly created App, click Verify
in order to verify the ownership of the App. You must be owner or administrator of the LinkedIn page in order to verify.
In the Verification page, click Generate URL
Visit the generated Verification URL
using the admin or onwer account of the company’s page in LinkedIn.
Click on Approve Verification
Make sure your App is verified
In your App, go to the Auth
tab, fill the Redirect URLs
field and click Update
Step 3 - Retrieve your Code
Visit the following URL, changing the values for CLIENT_ID
, REDIRECT_URL
and A_RANDOM_STRING
for the ones you created.
The random string is to avoid CSRF attacks.
1
https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&state=A_RANDOM_STRING&scope=r_emailaddress
Log in with your LinkedIN account and the redirected website will have your code in the URL:
Copy the Code part of the URL only and run the following CURL command replacing the values YOUR_CODE
, YOUR_CLIENT_ID
, YOUR_CLIENT_SECRET
and YOUR_REDIRECT_URI
for the values of your application
1
2
3
4
5
curl -X POST \
https://www.linkedin.com/oauth/v2/accessToken \
-H 'cache-control: no-cache' \
-H 'content-type: application/x-www-form-urlencoded' \
-d 'client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&redirect_uri=YOUR_REDIRECT_URI&code=YOUR_CODE&grant_type=authorization_code'
Run it and you should retrieve your access token:
REMEMBER: the code can be used only once. If you get an error or don’t use your token, you must re-generate your Code to be able to run it again.
Step 4 - Start the development
Now that the Sign In with LinkedIn is configured, you can start the development process.
The format for AUTHDATA is:
1
2
3
4
5
6
7
{
"linkedin": {
"id": "user's LinkedIn id (string)",
"access_token": "an authorized LinkedIn access token for the user",
"is_mobile_sdk": true|false // set to true if you acquired the token through LinkedIn mobile SDK
}
}
Here is the method for the iOS SDK:
1
2
3
PFUser.logInWithAuthType(inBackground: "linkedin", authData: ["access_token":tokenString, "id": user, "is_mobile_sdk": true]).continueWith { task -> Any? in
}
And here for the Android SDK:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Map<string, string, bool> authData = new HashMap<string, string, bool>();
authData.put("access_token", tokenString);
authData.put("id", user);
authData.put("is_mobile_sdk", true);
Task<ParseUser> t = ParseUser.logInWithInBackground("google", authData);
t.continueWith(new Continuation<ParseUser, Void>() {
public Void then(Task task) throws Exception {
if (task.isCancelled()) {
Log.w(TAG, "Task cancelled");
} else if (task.isFaulted()) {
Log.w(TAG, "Save FAIL" + task.getError());
Utilities.showToast(getResources().getString(R.string.errorLogin) + task.getError(), MainActivity.this);
} else {
// the object was saved successfully.
ParseUser user = (ParseUser)task.getResult();
Log.w(TAG, "Success " + user.getObjectId() + " " + user.getUsername() + " " + user.getEmail() + " " + user.getSessionToken());
}
}
}