Search code examples
javascriptjqueryhtmlrestlinkedin-api

Integrating this REST GET Request into JQuery or JavaScript


Note: Please be aware that I did not want to disclose my auth key or my company ID and so I have taken precautions to not display them.

I am struggling with Rest calls and reading JSON with a LinkedIn app I am trying to create for my website.

Using this console: https://apigee.com/console/linkedin I am able to request the data I need from a specific LinkedIn company page. However when trying to request the data without using the apigee console, I can't wrap my head around it.

What I need to do is turn the following REST call into a JQuery or JavaScript function on my .html page (on page load).

GET /v1/companies/(COMPANY ID)?oauth2_access_token=***************************&format=json 

HTTP/1.1

Host:api.linkedin.com

X-Target-URI: https://api.linkedin.com

Connection: Keep-Alive

Could someone please assist me in how to transform this into a proper query that initiates on page load?

Here is an example of something I tried, but did not work:

<script>
    $.ajax({
        url: 'https://api.linkedin.com/v1/companies/(COMPANY ID)?oauth2_access_token=***************************&format=json ',
        type: 'GET',
        Host: 'api.linkedin.com',
        Connection: 'Keep-Alive',
        success: function () {
            alert('GET completed');
        }
    });
</script>

Also I am using the proper GET request correct? The apigee console shows two requests but I am assuming this one:

GET https://api.linkedin.com/v1/companies/(COMPANY ID)?format=json

Is the unauthenticated request?

Thank you.


Solution

  • Just try jQuery's get function:

    https://api.jquery.com/jquery.get/

    something like this:

    function success(data){
       ///do stuff if it's ok
    }
    
    function error(data){
       ///do stuff if it's not ok
    }
    
    /// Define the URL
    url = 'https://api.linkedin.com/v1/companies/(COMPANY ID);
    
    /// Write an object with data to send
    sentData = {
       oauth2_access_token : "***************************"
      ,format: "json"
    };
    
    /// Finally perform the get request itself
    $.get(url, sentData)
       .done(success)
       .fail(error);
    

    This is almost what you need. Works well for me.