public class OAuthApiConnection extends 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
Modifier | Constructor and Description |
---|---|
|
OAuthApiConnection(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.
|
Modifier and Type | Method and Description |
---|---|
void |
checkCredentials()
Checks if the OAuth credentials (i.e., consumer key/secret and access token/secret) are still valid.
|
String |
getAccessSecret() |
String |
getAccessToken() |
protected okhttp3.OkHttpClient.Builder |
getClientBuilder()
Subclasses can customize their own
OkHttpClient.Builder instances. |
String |
getConsumerKey() |
String |
getConsumerSecret() |
String |
getCurrentUser()
Returns the username of the user who is currently logged in.
|
void |
logout()
Forgets the OAuth credentials locally.
|
checkErrors, getApiBaseUrl, getConnectTimeout, getReadTimeout, getTokens, implodeObjects, isLoggedIn, logWarnings, sendJsonRequest, sendJsonRequest, sendRequest, setConnectTimeout, setReadTimeout
public OAuthApiConnection(String apiBaseUrl, String consumerKey, String consumerSecret, String accessToken, String accessSecret)
ApiConnection.isLoggedIn()
will return true
until ApiConnection.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>)
.
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 processprotected OAuthApiConnection(String apiBaseUrl, String consumerKey, String consumerSecret, String accessToken, String accessSecret, String username, boolean loggedIn, Map<String,String> tokens, int connectTimeout, int readTimeout)
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 millisecondsprotected okhttp3.OkHttpClient.Builder getClientBuilder()
ApiConnection
OkHttpClient.Builder
instances.
An example:
return new OkHttpClient.Builder() .connectTimeout(5, TimeUnit.MILLISECONDS) .readTimeout(5, TimeUnit.MILLISECONDS) .cookieJar(...);
getClientBuilder
in class ApiConnection
public void logout()
logout
in class ApiConnection
public void checkCredentials() throws IOException, MediaWikiApiErrorException
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.
checkCredentials
in class ApiConnection
MediaWikiApiErrorException
- if the check failedIOException
public String getCurrentUser()
ApiConnection
getCurrentUser
in class ApiConnection
public String getConsumerKey()
public String getConsumerSecret()
public String getAccessToken()
public String getAccessSecret()
Copyright © 2014–2024 Wikidata Toolkit Developers. Generated from source code published under the Apache License 2.0. For more information, see the Wikidata Toolkit homepage