Name Last Update
src Loading commit data...
README.md Loading commit data...
pom.xml Loading commit data...

README.md

JAVA-ORCID-CLIENT

Build .jar client

You should use maven, version 3, to build the package. mvn clean package

A new jar file will be generated on ./target directory: degois-orcid-client-2.0-rc1.jar

This jar will include the source code as well as will build all POJO classes based on XSD schema files using JAXB.

Requirements to use this client in your project

Maven dependencies you will require

In Maven section you must indicate the versions of the dependencies

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<jersey.version>2.22.1</jersey.version>
<jaxb2-basics.version>0.9.5</jaxb2-basics.version>
<log4j.version>2.4</log4j.version>

In Maven section you must include the log4j, jersey client, jaxb, javax dependencies in your project

<!-- Log4J -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j.version}</version>
</dependency>

<!-- Jersey Client -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-jaxb</artifactId>
    <version>${jersey.version}</version>
</dependency>

<!-- JAXB2 Commons -->
<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics-runtime</artifactId>
    <version>${jaxb2-basics.version}</version>
</dependency>

<!-- javax -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0.1</version>
</dependency>

Java imports in your project

You should import:

// Import the client
import org.um.dsi.gavea.orcid.client.OrcidOAuthClient;
import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;

// Import activities
import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
import org.um.dsi.gavea.orcid.model.activities.Identifier;
import org.um.dsi.gavea.orcid.model.activities.WorkGroup;

// Import Works
import org.um.dsi.gavea.orcid.model.work.ExternalIdentifier;
import org.um.dsi.gavea.orcid.model.work.ExternalIdentifierType;
import org.um.dsi.gavea.orcid.model.work.Work;
import org.um.dsi.gavea.orcid.model.work.WorkExternalIdentifiers;
import org.um.dsi.gavea.orcid.model.work.WorkTitle;
import org.um.dsi.gavea.orcid.model.work.WorkType;

// Import Enum types
import org.um.dsi.gavea.orcid.model.common.RelationshipType;
import org.um.dsi.gavea.orcid.model.common.ScopePathType;

Usage Examples

Generate an Access Token, using the sandbox

String loginUri = "https://sandbox.orcid.org";
String clientSecret = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
String clientId = "XXXX-XXXX-XXXX-XXXX";
String apiUri = "https://api.sandbox.orcid.org";
//Use google OAUTH playground, this should be the landing page used to receive the Authorization Code
String redirectUri = "https://developers.google.com/oauthplayground";

// This scopes will allow you to do almost anything to the user profile 
// You should limit the scopes you want to use
List<ScopePathType> scopes = new ArrayList<ScopePathType>();
scopes.add(ScopePathType.AUTHENTICATE);
scopes.add(ScopePathType.ORCID_PROFILE_READ_LIMITED);
scopes.add(ScopePathType.ACTIVITIES_UPDATE);
scopes.add(ScopePathType.ORCID_BIO_UPDATE);

// Instantiate the Orcid Client
OrcidOAuthClient orcidClient = new OrcidOAuthClient(loginUri, apiUri, clientId, clientSecret, redirectUri); 

try {
        String authorizationURI = orcidClient.getAuthorizeUriLogin(scopes,state,orcid);
        System.out.println(authorizationURI);
} catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

Then you should have a landing page in your project, configured in redirectUri to get the Authentication Code.

OrcidAccessToken orcidToken;

try {
        orcidToken = orcidClient.getAccessTokens(authorizationCode);
        accessToken = orcidToken.getAccess_token(); 
        System.out.println(accessToken);

} catch (OrcidClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

if you already have a token, you could simply use setAccess_token method in the OrcidAccessToken object:

OrcidAccessToken orcidToken = new OrcidAccessToken();
orcidToken.setAccess_token("d3c1edb9-5385-4b76-af77-e3cd9889c5ec");

Get activities

To access Activities the token must be associated with the scope /orcid-profile/read-limited

try {
    ActivitiesSummary activities = orcidClient.readActivitiesSummary(orcidToken);
    List<WorkGroup> workGroupList = activities.getWorks().getGroup();

    for (WorkGroup workGroup : workGroupList) {             
        System.out.print("PUTCODE: " + workGroup.getWorkSummary().get(0).getPutCode() + " - ");
        System.out.println(workGroup.getWorkSummary().get(0).getTitle().getTitle());    

        List<Identifier> identifiersList = workGroup.getIdentifiers().getIdentifier();
        for (Identifier identifier: identifiersList) {  
            //print ID_TYPE: ID_VALUE
            System.out.print(identifier.getExternalIdentifierType() +": " + identifier.getExternalIdentifierId()+"\n");
        }
    }
} catch (OrcidClientException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Get a Work by PUTCODE

try {
    String putCode = "477804";

    Work work = orcidClient.readWork(orcidToken, putCode);
} catch (OrcidClientException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Update a Work

To update a work the token used must be associated with one of the scopes: /activities/update or /orcid-works/update

try {
    String putCode = "477804";

    Work work = orcidClient.readWork(orcidToken, putCode);
    WorkTitle workTitle = new WorkTitle("Changed title regarding PTCRISSync",null,null); 
    work.setTitle(workTitle);

    //Save work with changed title
    orcidClient.updateWork(orcidToken2, putCode, work);         
    System.out.println("\n\nSaved with title: " + workTitle.getTitle());

} catch (OrcidClientException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Create a new Work

To create a new work the token used must be associated with one of the scopes: /activities/update or /orcid-works/create

Work newWork = new Work();

// Set the title
WorkTitle workTitle2 = new WorkTitle("new item PTCRISSync",null,null);
newWork.setTitle(workTitle2);           

// Set the type     
newWork.setType(WorkType.JOURNAL_ARTICLE);

// Set External ID (ORCID API requires at least one)
ExternalIdentifier internalId = new ExternalIdentifier();
internalId.setExternalIdentifierType(ExternalIdentifierType.SOURCE_WORK_ID);
internalId.setExternalIdentifierId("PTCRIS_01");
// You must indicate the Relationship or the External URL
internalId.setRelationship(RelationshipType.SELF);

// Set the External Identifiers 
WorkExternalIdentifiers externalIdentifiers = new WorkExternalIdentifiers();
externalIdentifiers.getWorkExternalIdentifier().add(internalId);
newWork.setExternalIdentifiers(externalIdentifiers);

try {

    //The method addWork will retrieve the PUTCODE
    String newPutCode = orcidClient.addWork(orcidToken2, newWork);          
    System.out.println("New item with PUTCODE: " + newPutCode);

} catch (OrcidClientException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}    

License

Please contact us.