linkedin_auth 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 75

LinkedIn Auth (Unofficial) #

Flutter auth for LinkedIn using V2 APIs and OAuth 2.0

Getting Started #

Registering as a developer on LinkedIn #

  • To get started, first you must create an account on: https://www.linkedin.com/developers/login
  • Create an app, and complete necessary steps
  • Setup redirect url on Auth Tab -> OAuth 2.0 Settings -> Redirect URLs
  • Collect clientId and clientSecret from Auth Tab -> Application Credentials

Authenticating with LinkedIn in Flutter app #

Before using auth, there are two methods to complete the third step for 3-legged auth. It's important to know that if you ship a client secret with your app, you must make sure that its either kept very secure or the app is not shipped for general public. One method just sends the client secret from the app itself, and the other method expects you to have a service that does it for you and provide a method to parse that response.

For server side authentication, you must have a server that when called with query params, code and state, returns an access token by completing the third leg in the 3-legged auth explained here: https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context#step-3-exchange-authorization-code-for-an-access-token

Once these are setup, the code usually looks like this:

FlatButton(
  onPressed: () async {
    await Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => Scaffold(
              appBar: AppBar(
                leading: CloseButton(),
              ),
              body: LinkedInLoginView(
                clientId: "what_an_id",
                redirectUrl: "https://www.example.com",
                onError: (String error) {
                  print(error);
                },
                onServerResponse: (res){
                  var parsed = json.decode(res.body);
                  return AccessToken(parsed["token"], parsed["expiry"]),
                },
                onTokenCapture: (token) {
                  this.token = token.token;
                  Navigator.pop(context, token);
                },
              ))),
    );
    print("Ended, must've gotten result here: ${this.token}");
  },
  child: Text("Signup/Login"),
),

After capturing token, one can simply use it to fetch profile, email address and display image.

Client side authentication [UNSAFE]

For client side authentication, you must ship the client secret with the app itself. You can do the auth simply as follows:

FlatButton(
  onPressed: () async {
    await Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => Scaffold(
              appBar: AppBar(
                leading: CloseButton(),
              ),
              body: LinkedInLoginView(
                clientId: "what_an_id",
                redirectUrl: "https://www.example.com",
                onError: (String error) {
                  print(error);
                },
                bypassServerCheck: true,
                clientSecret: "what_a_secret",
                onTokenCapture: (token) {
                  this.token = token.token;
                  Navigator.pop(context, token);
                },
                onServerResponse: (res){
                  var parsed = json.decode(res.body);
                  return AccessToken(parsed["token"], parsed["expiry"]),
                },
              ))),
    );
    print("Ended, must've gotten result here: ${this.token}");
  },
  child: Text("Signup/Login"),
),

After the end on either of the two methods, you get an AccessToken which you can use to fetch data from LinkedIn. You can also provide multiple scopes manually to LinkedInLoginView. Normally two scopes are requested r_liteprofile and r_emailaddress. Basic profile is shown in access but is non accessible by developers without special access from LinkedIn.

Getting data from LinkedIn: Profile, Email Address, Display image #

Getting the lite profile & display image

FlatButton(
  onPressed: () async {
    try{
      var liteProfile =await LinkedInService.getLiteProfile(token);
      print(liteProfile);
      var profileImage = await liteProfile.profileImage.getDisplayImageUrl(token);
      print(profileImage);
    } on LinkedInException catch(e){
      print(e.cause);
    }
  },
  child: Text("Get Lite Profile"),
),

Note: Display image is only 100X100 in resolution. Original image requires special access from linkedIn.

Getting email address #

FlatButton(
  onPressed: () async {
    var email = await LinkedInService.getEmailAddress(token);
    print(email);
  },
  child: Text("Get Email address"),
),

Get Basic Profile (Special permission required) #

FlatButton(
  onPressed: () async {
    try{
      var basicProfile =await LinkedInService.getBasicProfile(token);
      print(basicProfile);
      var profileImage = await basicProfile.profileImage.getDisplayImageUrl(token);
      print(profileImage);
    } on LinkedInException catch(e){
      print(e.cause);
    }
  },
  child: Text("Get Basic Profile"),
),

Refreshing Token #

Programmatic refresh of tokens is only available to select developers, hence not included in this package. For normal refresh of token, one must check expiry of the access token and re-run the Auth flow before it expires. If the token is not expired, it will skip the login view and directly give you a new token. If it has expired, the login page will appear and user has to login again.

Read more: https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context#step-5-refresh-access-token

[0.0.3] - 21/06/2019

  • Formatting changes, package upgrades

[0.0.2] - 21/06/2019

  • Updated dependencies
  • Removed redundant logging
  • Created a new client side token capture flutter example

[0.0.1] - 27/03/2019

  • Added ability to login/signup using LinkedIn
  • Support for fetching access token locally using client secret
  • Support for server method to parse access token from redirect URL
  • Methods to fetch r_basicprofile, r_liteprofile & r_emailaddress
  • Support for fetching displayImage from URN

example/README.md

example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  linkedin_auth: ^0.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:linkedin_auth/linkedin_auth.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
53
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
http ^0.12.0+2 0.12.0+2
uuid ^2.0.2 2.0.2
webview_flutter ^0.3.9+1 0.3.15+1
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.3
http_parser 3.1.3
meta 1.1.7
path 1.6.4
pedantic 1.8.0+1
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test