README.md 7.38 KB

JAVA-ORCID-CLIENT

Usage Examples

Maven dependencies

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>

Requirements

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;

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:

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

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

try {
    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);

    //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.