README.md 5.55 KB

JAVA-ORCID-CLIENT

Usage Examples

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.