Oauth 2.0 authorization for LinkedIn in Android

前端 未结 4 1565
借酒劲吻你
借酒劲吻你 2020-11-27 15:07

Even though there is no such android specific sdk from linkedIn(like facebook and twitter sdk for android).Setting up linkedIn authorization with Oauth 1.0 was still easy us

4条回答
  •  我在风中等你
    2020-11-27 15:08

    I got it working, but it took me... some time.

    I followed LinkedIn Authentication to manage that.
    I still strongly advice to still read this link, as I do not cover all the cases in my examples (errors, error handling, best pratices, parameters usage, precise documentation...)

    • First, you need to have your LinkedIn API Key and Secret Key. If you don't, register an app on here.

    • Second, you need an Activity in the application that can receive the authorization code. For that, it needs to be set as browsable (launchable from a browser) in the AndroidManifest.xml file :

          
            
              
      
              
              
            
      

      Although not recommended, it's possible to use a data tag to retrieve URIs using a custom scheme :

        
      
    • After that, you need to redirect the user to the LinkedIn's authorization dialog, using a specific URL :

      https://www.linkedin.com/uas/oauth2/authorization?response_type=code
                                         &client_id=YOUR_API_KEY
                                         &scope=SCOPE 
                                         &state=STATE
                                         &redirect_uri=YOUR_REDIRECT_URI
      

      You can use a WebView to directly show it in your application, or let the system handle it through an Intent like :

      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(/* FULL URL */));
      startActivity(intent);
      

      The only problem here is that the API does not accept schemes other than http or https, meaning you can't just pass the intent URI as the redirect_uri parameter.

      So I created a landing page on my server, with only purpose is to redirect to the application. We can imagine something like (in ugly shorten PHP) (Intent ref.) :

      header('Location: ' . "intent:#Intent;component=your.package/.ResultActivity;S.code=" . $_GET['code'] . ";S.state=" . $_GET['state'] . ";end");
      die();
      

      So everything's set! Now the onCreate(Bundle) of the ResultActivity :

      Intent intent = getIntent();
      String authorizationCode = intent.getStringExtra("code");
      

      There is another way to pass parameters here, if the data tag was used earlier.

    • Almost there! Now you just need to perform a simple POST request on that URL :

      https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code
                                          &code=AUTHORIZATION_CODE
                                          &redirect_uri=YOUR_REDIRECT_URI
                                          &client_id=YOUR_API_KEY
                                          &client_secret=YOUR_SECRET_KEY
      

      Returning a JSON object on success :

      {"expires_in":5184000,"access_token":"AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mff4gPODzOYR"}

    Et voilà ! You can now make your API calls using the access_token. Don't forget to store it somewhere so you don't have through these steps again.

    I hope this wasn't too long to read and that it can help some people. :)

提交回复
热议问题