[ORCID API]Use java httpclient to get Access Token

1,710 views
Skip to first unread message

mayh

unread,
Feb 13, 2014, 8:14:06 PM2/13/14
to orcid-a...@googlegroups.com
Hi,

 I'd write a thin program base on java to integrate our library site.

 But on the first step, I got the error message "
{"error":"unauthorized","error_description":"An Authentication object was not  found in the SecurityContext"}. "

 What's wrong with me?  Here is my code.


            DefaultHttpClient httpclient = new DefaultHttpClient();            
            HttpContext httpContext = new BasicHttpContext();
            this.cookieStore = new BasicCookieStore();
            httpContext.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore);
           
            HttpPost httppost = new HttpPost("http://api.sandbox-1.orcid.org/oauth/token");
            httppost.addHeader("Accept","application/json");
            java.util.List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("client_id", clientId));
            nvps.add(new BasicNameValuePair("client_secret", secret));
            nvps.add(new BasicNameValuePair("scope", "/read-public"));
            nvps.add(new BasicNameValuePair("grant_type", "client_credentials"));
            StringEntity stringentity = new StringEntity(URLEncodedUtils.format(nvps, "UTF-8"));
            httppost.setEntity(stringentity);

            String result = EntityUtils.toString(httpclient.execute(httppost,httpContext).getEntity(),"UTF-8"); 
// Just print out the result.

Shun-hong
--
未經授課教師允許,學生不應於任何場合引用或轉述老師和同學上課發言意見。尊重學術
倫理,並忌天馬行空,以管窺天。
--
不在圖書館,就在往圖書館的路上~~~>"<
--

Peters, Robert

unread,
Feb 14, 2014, 8:21:13 AM2/14/14
to mayh, orcid-a...@googlegroups.com
Hi Shun-hong,
That error looks the same as not posting a form.

curl -i -v -H 'Accept: application/json' -d 'client_id=0000-0002-9353-5519&client_secret=1470a572-1e0d-4e7d-899a-c9eafca4e05d&scope=/read-public&grant_type=client_credentials' 'http://api.sandbox-1.orcid.org/oauth/token'

vs

curl -i -L -H 'Accept: application/json' 'http://api.sandbox-1.orcid.org/oauth/token'

Looking at https://hc.apache.org/httpcomponents-client-ga/quickstart.html I would be temped use UrlEncodedFormEntity, but that would just be a guess.

Cheers,
Rob




--
You received this message because you are subscribed to the Google Groups "ORCID API Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orcid-api-use...@googlegroups.com.
To post to this group, send email to orcid-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/orcid-api-users.
For more options, visit https://groups.google.com/groups/opt_out.



--

Robert Peters
Lead Developer at ORCID.org

Cellphone: +1.805.440.9056
Skype: rcpeters
Timezone: PST

Suman Upadhyay

unread,
Feb 14, 2014, 8:32:52 AM2/14/14
to Peters, Robert, mayh, orcid-a...@googlegroups.com
Hi,
I encountered this error too. It was because of missing form parameters. Eventhough I had all the listed params, somehow it was not getting posted to orcid.

 I made it work using org.apache.http.client.methods.HttpPost and org.apache.http.NameValuePair

Thanks

Suman
Thank you,

Suman Upadhyay

mayh

unread,
Feb 15, 2014, 6:31:20 AM2/15/14
to Suman Upadhyay, Peters, Robert, orcid-a...@googlegroups.com
Hi, Suman
 
 Did you solve this problem? I'd follow the guide by put all need parameters, even try to simulate the cURL agent header.

 But it's still no work.

Shun-hong
On Fri, 14 Feb 2014 08:32:52 -0500, Suman Upadhyay wrote

Peters, Robert

unread,
Feb 15, 2014, 9:10:06 AM2/15/14
to mayh, Suman Upadhyay, orcid-a...@googlegroups.com
Hi Mayh,
Maybe you should try a different library.  Using ClientResource from Restlet looks nice:

Suman Upadhyay

unread,
Feb 15, 2014, 9:32:00 AM2/15/14
to mayh, Peters, Robert, orcid-a...@googlegroups.com
Yes mayh

I was able to connect to orcid from my application using the libraries i mentioned before. Right now we are only building a service to get back orcid id. Let me know if you have anymore question.

Sent from my iPhone

mayh

unread,
Feb 17, 2014, 12:02:15 AM2/17/14
to Suman Upadhyay, Peters, Robert, orcid-a...@googlegroups.com
Hi, Suman

 Because of  I want to  make our Web-Site simple with don't include too many framework.

 I prefer to write a new one. It was success when I change the library to newest Apache http-client library.

Shun-hong.

On Sat, 15 Feb 2014 09:32:00 -0500, Suman Upadhyay wrote

Demeranville, Tom

unread,
Feb 17, 2014, 8:34:43 AM2/17/14
to Peters, Robert, mayh, Suman Upadhyay, orcid-a...@googlegroups.com

The library as a whole is really easy to use.  It'll handle the OAuth part, generate auth requests and turning authcodes into tokens.  It'll handle search, profile reads and updating works.  Adding other calls to the API should be really easy.  Oh, and it can be added with Maven.  See https://github.com/TomDemeranville/orcid-java-client for docs and maven instructions.

Turning an auth code into a token is as simple as creating a client using your credentials and calling a method.

OrcidOAuthClient client = new OrcidOAuthClient("OrcidClientID","OrcidClientSecret",("OrcidReturnURI"),useSandbox);
OrcidAccessTokenResponse token = client.getAccessToken(authCode);

What are you trying to achieve?  I could probably add the call to the library.

Tom.

                        > ????????,??????????????????????????????
                        > ??,??????,?????
                        > --
                        > ?????,?????????~~~>"<


                        > --
                        >
                        > --
                        > You received this message because you are subscribed to the Google Groups "ORCID API Users" group.

                        > To unsubscribe from this group and stop receiving emails from it, send an email to orcid-api-use...@googlegroups.com <mailto:orcid-api-users%2Bunsu...@googlegroups.com> .


                        > To post to this group, send email to orcid-a...@googlegroups.com.
                        > Visit this group at http://groups.google.com/group/orcid-api-users.
                        > For more options, visit https://groups.google.com/groups/opt_out.
                        >


                >
               
                >
                > --
                >
                > Robert Peters

                > Lead Developer at ORCID.org <http://orcid.org/
                >
                > Cellphone: +1.805.440.9056 <tel:%2B1.805.440.9056> 


                >
                > Email: r.pe...@orcid.org
                > Skype: rcpeters
                > Timezone: PST
                >
                > --
                > You received this message because you are subscribed to the Google Groups "ORCID API Users" group.

                > To unsubscribe from this group and stop receiving emails from it, send an email to orcid-api-use...@googlegroups.com <mailto:orcid-api-users%2Bunsu...@googlegroups.com> .


                > To post to this group, send email to orcid-a...@googlegroups.com.
                > Visit this group at http://groups.google.com/group/orcid-api-users.
                > For more options, visit https://groups.google.com/groups/opt_out.
                >


        >
       
        >
        > --
        > Thank you,
        >
        > Suman Upadhyay
        >
        > --
        > You received this message because you are subscribed to the Google Groups "ORCID API Users" group.

        > To unsubscribe from this group and stop receiving emails from it, send an email to orcid-api-use...@googlegroups.com <mailto:orcid-api-users%2Bunsu...@googlegroups.com> .


        > To post to this group, send email to orcid-a...@googlegroups.com.
        > Visit this group at http://groups.google.com/group/orcid-api-users.
        > For more options, visit https://groups.google.com/groups/opt_out.
       
       
        --

        ????????,??????????????????????????????
        ??,??????,?????
        --
        ?????,?????????~~~>"<


        --
       
       



--


Robert Peters
Lead Developer at ORCID.org

Cellphone: +1.805.440.9056

Email: r.pe...@orcid.org
Skype: rcpeters
Timezone: PST

Message has been deleted

miguelangelluc

unread,
Feb 20, 2014, 6:54:27 AM2/20/14
to orcid-a...@googlegroups.com, ma...@ntnu.edu.tw
Hi all, and sorry for my english :P

I had the same problem, with java.net.* (HttpURLConnection for example) doesnt work, but (like someone here said) with apache HttpComponents (Http Client 4.3) works fine --> http://hc.apache.org/httpcomponents-client-4.3.x/index.html 



A example of implementation for adquire a ORICD with a correct Code will be:

You will need these imports:

import org.apache.http.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.util.*;
import org.apache.http.message.*;
import org.apache.http.client.entity.*;

the implementation (with json simple for the response https://code.google.com/p/json-simple/):
 

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost hp=new HttpPost(url_token);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
 
nameValuePairs.add(new BasicNameValuePair("client_id", client_id)); 
nameValuePairs.add(new BasicNameValuePair("client_secret", client_secret));
        nameValuePairs.add(new BasicNameValuePair("grant_type", "authorization_code"));  
nameValuePairs.add(new BasicNameValuePair("code", code)); 
        nameValuePairs.add(new BasicNameValuePair("redirect_uri",url_response )); 
        hp.setEntity(new UrlEncodedFormEntity(nameValuePairs));    
        
        HttpResponse response = httpclient.execute(hp);         
    
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";   
        
        JSONParser parser=new JSONParser();         
        if ((line = rd.readLine()) != null) {
          //This line is the json response of the server :)
        System.out.println(line);
         
        Object obj = parser.parse(line);
        JSONObject obj2 = (JSONObject)obj;
         
           
            System.out.println("access_token:"+obj2.get("access_token"));
            System.out.println("token_type:"+obj2.get("token_type"));
            System.out.println("efresh_token:"+obj2.get("efresh_token"));
            System.out.println("expires_in:"+obj2.get("expires_in"));
            System.out.println("scope:"+obj2.get("scope"));
            System.out.println("orcid:"+obj2.get("orcid"));
            orcid=(String)obj2.get("orcid");
             
        
            
        }


Regards!;

sba...@kevol.com

unread,
Feb 20, 2014, 11:56:09 AM2/20/14
to orcid-a...@googlegroups.com
I don't want to muddy the waters here, but I am using HttpURLConnection to get the OAuth token.

Cheers,
Sidney
--
Reply all
Reply to author
Forward
0 new messages