Class OAuthApiConnection
- Author:
- Antonin Delpeuch, Lu Liu
-
Field Summary
Fields inherited from class org.wikidata.wdtk.wikibaseapi.ApiConnection
apiBaseUrl, ASSERT_PARAMETER, connectTimeout, loggedIn, PARAM_ACTION, PARAM_FORMAT, readTimeout, tokens, URL_TEST_WIKIDATA_API, URL_WIKIDATA_API, URL_WIKIMEDIA_COMMONS_API, URLENCODED_MEDIA_TYPE, username
-
Constructor Summary
ModifierConstructorDescriptionOAuthApiConnection
(String apiBaseUrl, String consumerKey, String consumerSecret, String accessToken, String accessSecret) Constructs an OAuth connection to the given MediaWiki API endpoint.protected
OAuthApiConnection
(String apiBaseUrl, String consumerKey, String consumerSecret, String accessToken, String accessSecret, String username, boolean loggedIn, Map<String, String> tokens, int connectTimeout, int readTimeout) Deserializes an existing OAuthApiConnection from JSON. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Checks if the OAuth credentials (i.e., consumer key/secret and access token/secret) are still valid.protected okhttp3.OkHttpClient.Builder
Subclasses can customize their ownOkHttpClient.Builder
instances.Returns the username of the user who is currently logged in.void
logout()
Forgets the OAuth credentials locally.Methods inherited from class org.wikidata.wdtk.wikibaseapi.ApiConnection
checkErrors, getApiBaseUrl, getConnectTimeout, getReadTimeout, getTokens, implodeObjects, isLoggedIn, logWarnings, sendJsonRequest, sendJsonRequest, sendRequest, setConnectTimeout, setReadTimeout
-
Constructor Details
-
OAuthApiConnection
public OAuthApiConnection(String apiBaseUrl, String consumerKey, String consumerSecret, String accessToken, String accessSecret) Constructs an OAuth connection to the given MediaWiki API endpoint.ApiConnection.isLoggedIn()
will return true untilApiConnection.logout()
is called.NOTICE: The constructor doesn't check if the OAuth credentials (i.e., the consumer key/secret and the access token/secret) are valid. Even if the credentials are valid when calling this constructor, they can be revoked by the user at any time.
The validity of the credentials is automatically checked if you use
ApiConnection.sendJsonRequest(java.lang.String, java.util.Map<java.lang.String, java.lang.String>)
.- Parameters:
apiBaseUrl
- the MediaWiki API endpoint, such as "https://www.wikidata.org/w/api.php"consumerKey
- the OAuth 1.0a consumer keyconsumerSecret
- the OAuth 1.0a consumer secretaccessToken
- the access token obtained via the OAuth processaccessSecret
- the secret key obtained via the OAuth process
-
OAuthApiConnection
protected OAuthApiConnection(String apiBaseUrl, String consumerKey, String consumerSecret, String accessToken, String accessSecret, String username, boolean loggedIn, Map<String, String> tokens, int connectTimeout, int readTimeout) Deserializes an existing OAuthApiConnection from JSON.- Parameters:
apiBaseUrl
- the MediaWiki API endpoint, such as "https://www.wikidata.org/w/api.php"consumerKey
- the OAuth 1.0a consumer keyconsumerSecret
- the OAuth 1.0a consumer secretaccessToken
- the access token obtained via the OAuth processaccessSecret
- the secret key obtained via the OAuth processusername
- name of the current userloggedIn
- true if login succeeded.tokens
- map of tokens used for this sessionconnectTimeout
- the maximum time to wait for when establishing a connection, in millisecondsreadTimeout
- the maximum time to wait for a server response once the connection was established, in milliseconds
-
-
Method Details
-
getClientBuilder
protected okhttp3.OkHttpClient.Builder getClientBuilder()Description copied from class:ApiConnection
Subclasses can customize their ownOkHttpClient.Builder
instances. An example:return new OkHttpClient.Builder() .connectTimeout(5, TimeUnit.MILLISECONDS) .readTimeout(5, TimeUnit.MILLISECONDS) .cookieJar(...);
- Specified by:
getClientBuilder
in classApiConnection
-
logout
public void logout()Forgets the OAuth credentials locally. No requests will be made.- Specified by:
logout
in classApiConnection
-
checkCredentials
Checks if the OAuth credentials (i.e., consumer key/secret and access token/secret) are still valid.The OAuth credentials can be invalid if the user invoked it.
We simply call
ApiConnection.checkCredentials()
here. Because for OAuth, the query "action=query&assert=user" returns success if and only if the credentials are still valid. This behaviour is the same when using username/password for logging in.This method throws
AssertUserFailedException
if the check failed. This does not update the state of the connection object.- Overrides:
checkCredentials
in classApiConnection
- Throws:
MediaWikiApiErrorException
- if the check failedIOException
-
getCurrentUser
Description copied from class:ApiConnection
Returns the username of the user who is currently logged in. If there is no user logged in the result is an empty string.- Overrides:
getCurrentUser
in classApiConnection
- Returns:
- name of the logged in user
-
getConsumerKey
-
getConsumerSecret
-
getAccessToken
-
getAccessSecret
-