Blame view

README.md 9.35 KB
958d8db3c   Paulo Graça   Initial documenta...
1
  # JAVA-ORCID-CLIENT
75cef0137   Paulo Graça   Changes in introd...
2
3
  Simple, easy to use ORCiD client written in Java. Supports the public and Tier2 API with OAuth. Natural object mapping - The entire ORCiD message schema is represented as a hirearchical graph of POJOs with JAXB support for serialisation.
  This work is based on Tom Demeranville Orcid Java Client that is available at [GitHub](https://github.com/TomDemeranville/orcid-java-client)
0e6d4def5   Paulo Graça   change build text
4
  # Build .jar client
b2cfa1538   Paulo Graça   Changing build text
5
6
  
  You should use maven, version 3, to build the package.
d551bd5bb   Paulo Graça   update introduction
7
8
  
      mvn clean package
b2cfa1538   Paulo Graça   Changing build text
9
10
    
  A new jar file will be generated on ./target directory:
d551bd5bb   Paulo Graça   update introduction
11
12
  
      degois-orcid-client-2.0-rc1.jar
b2cfa1538   Paulo Graça   Changing build text
13
75cef0137   Paulo Graça   Changes in introd...
14
  This jar will include the source code as well as will build all POJOs classes based on XSD schema files using JAXB.
b2cfa1538   Paulo Graça   Changing build text
15
4b327300c   Paulo Graça   Change doc structure
16
bf0a167bf   Paulo Graça   Change text
17
  # Requirements to use this client in your project
958d8db3c   Paulo Graça   Initial documenta...
18
bf0a167bf   Paulo Graça   Change text
19
  ## Maven dependencies you will require
939e12287   Paulo Graça   Add the dependencies
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  
  In Maven <properties> 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 <dependencies> 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>
9c05b8720   Paulo Graça   deploy section
80
81
82
83
84
85
86
87
88
89
      
      
  ## Maven deploy
  
  It will deploy your jar into your localhost /tmp/repo path:
  
      mvn deploy:deploy-file -Durl=file:///tmp/repo/ \
                         -DrepositoryId=localhost \
                         -Dfile=target/degois-orcid-client-2.0-rc1.jar \
                         -DpomFile=pom.xml
939e12287   Paulo Graça   Add the dependencies
90
4b327300c   Paulo Graça   Change doc structure
91
  ## Java imports in your project
958d8db3c   Paulo Graça   Initial documenta...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  
  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;
      
4b327300c   Paulo Graça   Change doc structure
117
  # Usage Examples
958d8db3c   Paulo Graça   Initial documenta...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  
  ## 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();
  	}
  	
b0d3432ab   Paulo Graça   Complete with cod...
146
  Then you should have a landing page in your project, configured in redirectUri to get the Authentication Code.
958d8db3c   Paulo Graça   Initial documenta...
147
148
149
150
151
152
153
154
155
156
157
158
  
      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();
  	}
b0d3432ab   Paulo Graça   Complete with cod...
159
160
  
  if you already have a token, you could simply use setAccess_token method in the OrcidAccessToken object:
be412ccf3   Paulo Graça   changes in text
161
  	OrcidAccessToken orcidToken = new OrcidAccessToken();
b0d3432ab   Paulo Graça   Complete with cod...
162
  	orcidToken.setAccess_token("d3c1edb9-5385-4b76-af77-e3cd9889c5ec");
958d8db3c   Paulo Graça   Initial documenta...
163
  ## Get activities
be412ccf3   Paulo Graça   changes in text
164
  To access Activities the token must be associated with the scope /orcid-profile/read-limited
958d8db3c   Paulo Graça   Initial documenta...
165
b0d3432ab   Paulo Graça   Complete with cod...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
      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()+"
  ");
  			}
          }
      } catch (OrcidClientException e) {
  		// TODO Auto-generated catch block
  		e.printStackTrace();
  	}
a31a6e269   Paulo Graça   Add example output
185
186
187
188
189
190
191
192
193
194
195
196
  	
  The output will be:
  
      PUTCODE: 465120 - Organic matter composition and macrofaunal diversity in sediments of the Condor Seamount (Azores, {NE} Atlantic)
      doi: http://dx.doi.org/10.1016/j.dsr2.2013.08.006
      PUTCODE: 468006 - A complex picture of associations between two host mussels and symbiotic bacteria in the Northeast Atlantic
      uri: http://hdl.handle.net/123456789/1154
      PUTCODE: 477773 - Shape analysis of an adaptive elastic rod model
      uri: http://hdl.handle.net/10400.19/2601
      doi: 10.1137/040604443
      PUTCODE: 477774 - Early skin immunological disturbance after Plasmodium-infected mosquito bites
      uri: http://hdl.handle.net/10400.7/254
b0d3432ab   Paulo Graça   Complete with cod...
197
198
199
200
201
202
203
204
205
206
207
208
  
  ## 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();
  	}
  	
958d8db3c   Paulo Graça   Initial documenta...
209
  ## Update a Work
be412ccf3   Paulo Graça   changes in text
210
  To update a work the token used must be associated with one of the scopes: /activities/update  or /orcid-works/update
b0d3432ab   Paulo Graça   Complete with cod...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      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("
  
  Saved with title: " + workTitle.getTitle());
  		
      } catch (OrcidClientException e) {
  		// TODO Auto-generated catch block
  		e.printStackTrace();
  	}
  	
958d8db3c   Paulo Graça   Initial documenta...
229
  ## Create a new Work
be412ccf3   Paulo Graça   changes in text
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  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);
b0d3432ab   Paulo Graça   Complete with cod...
252
      try {
b0d3432ab   Paulo Graça   Complete with cod...
253
254
255
256
257
258
259
260
261
  
          //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();
  	}    
958d8db3c   Paulo Graça   Initial documenta...
262
b0d3432ab   Paulo Graça   Complete with cod...
263
  # License
be412ccf3   Paulo Graça   changes in text
264
b0d3432ab   Paulo Graça   Complete with cod...
265
  Please contact us.