views:

358

answers:

2

Hi.

I'm developing (trying for now) portlet that will be integrated with LinkedIn.

Following the documentation about it: http://developer.linkedin.com/docs/DOC-1008 --> The first step to authorizing a LinkedIn member is requesting a requestToken. This request is done with an HTTP POST. For the requestToken step, the following components should be present in your string to sign:

* HTTP Method (POST)
* Request URI (https://api.linkedin.com/uas/oauth/requestToken)
* oauth_callback
* oauth_consumer_key
* oauth_nonce
* oauth_signature_method
* oauth_timestamp
* oauth_version

I have already API(it's *oauth_consumer_key*) key and i need to generate specific URL string. Have next java code for this URL and HTTP connection:

private void processAuthentication() {

    Calendar cal = Calendar.getInstance();
    Long ms = cal.getTimeInMillis();
    Long timestamp = ms / 1000;

    Random r = new Random();
    Long nonce = r.nextLong();

    String prefixUrl = "https://api.linkedin.com/uas/oauth/requestToken";
    String oauthCallback = "oauth_callback=http://localhost/";
    String oauthConsumerKey =
            "&oauth_consumer_key=my_consumer_key";
    String oauthNonce = "&oauth_nonce=" + nonce.toString();
    String oauthSignatureMethod = "&oauth_signature_method=HMAC-SHA1";
    String oauthTimestamp = "&oauth_timestamp=" + timestamp.toString();
    String oauthVersion = "&oauth_version=1.0";

    String mainUrl =
            oauthCallback + oauthConsumerKey + oauthNonce + oauthSignatureMethod
                    + oauthTimestamp + oauthVersion;

    try {
        prefixUrl =
                URLEncoder.encode(prefixUrl, "UTF-8") + "&"
                        + URLEncoder.encode(mainUrl, "UTF-8");

        URL url = new URL(prefixUrl);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        String msg = connection.getResponseMessage();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

The question is next,for those, who had faced this problem: How should really look URL string for connection and how response is received?

For URL, it's interested the example of URL, you generated. And for response interested, method to get it. As i understand, after HTTP connection been established,that response is:

connection.getResponseMessage();
+1  A: 

You might try out the OAuth libraries to handle the connection: http://code.google.com/p/oauth/

AJK
first that i need from this repository is:/code/java/core/consumer ?
sergionni
or i should download all java related and make jar with .pom?
sergionni
+1  A: 

@sergionni I found answer to your Question from linkedin-developer

As you know The first step to authorizing a Linked-In member is requesting a requestToken. This request is done with an HTTP POST.

Your base string should end up looking something like this if you're using a callback:

POST&https%3A%2F%2Fapi.linkedin.com%2Fuas%2Foauth%2FrequestToken &oauth_callback%3Dhttp%253A%252F%252Flocalhost%252Foauth_callback%26o auth_consumer_key%3DABCDEFGHIJKLMNOPQRSTUVWXYZ%26 oauth_nonce%3DoqwgSYFUD87MHmJJDv7bQqOF2EPnVus7Wkqj5duNByU%26 oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1259178158%26 oauth_version%3D1.0

You then sign this base string with your consumer_secret, computing a signature. In this case, if your secret was 1234567890, the signature would be TLQXuUzM7omwDbtXimn6bLDvfF8=.

Now you take the signature you generated, along with oauth_nonce, oauth_callback, oauth_signature_method, oauth_timestamp, oauth_consumer_key, and oauth_version and create an HTTP Authorization header. For this request, that HTTP header would look like:

Authorization: OAuth oauth_nonce="oqwgSYFUD87MHmJJDv7bQqOF2EPnVus7Wkqj5duNByU", oauth_callback="http%3A%2F%2Flocalhost%2Foauth_callback", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1259178158", oauth_consumer_key="ABCDEFGHIJKLMNOPQRSTUVWXYZ", oauth_signature="TLQXuUzM7omwDbtXimn6bLDvfF8=", oauth_version="1.0"

Please note, that the HTTP header is a single header -- not an HTTP header for each component. You can optionally supply a realm="http://api.linkedin.com".

As a response to your request for a requestToken, your requestToken will be in the "oauth_token" response field, a validation that we acknowledged your callback with the "oauth_callback_confirmed" field, an oauth_token_secret, and a oauth_expires_in, and a few other values.

(here us Your answaer) response would look like:

oauth_token=94ab03c4-ae2c-45e4-8732-0e6c4899db63 &oauth_token_secret=be6ccb24-bf0a-4ea8-a4b1-0a70508e452b &oauth_callback_confirmed=true&oauth_expires_in=599

Vaibhav Bhalke
if you have Sample Java code(with description like above) for next step like* Redirect the User to Linkedin Authorization Server* Request the access token* How to make 1st API request to getting Users info then drop me email on [email protected] Hope for Best co-operation from your side
Vaibhav Bhalke
i resolved this question, anyway thank you
sergionni