Class OAuthApiConnection

java.lang.Object
org.wikidata.wdtk.wikibaseapi.ApiConnection
org.wikidata.wdtk.wikibaseapi.OAuthApiConnection

public class OAuthApiConnection extends ApiConnection
A connection to the MediaWiki/Wikibase API which uses OAuth for authentication.
Author:
Antonin Delpeuch, Lu Liu
  • 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 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>).

      Parameters:
      apiBaseUrl - the MediaWiki API endpoint, such as "https://www.wikidata.org/w/api.php"
      consumerKey - the OAuth 1.0a consumer key
      consumerSecret - the OAuth 1.0a consumer secret
      accessToken - the access token obtained via the OAuth process
      accessSecret - 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 key
      consumerSecret - the OAuth 1.0a consumer secret
      accessToken - the access token obtained via the OAuth process
      accessSecret - the secret key obtained via the OAuth process
      username - name of the current user
      loggedIn - true if login succeeded.
      tokens - map of tokens used for this session
      connectTimeout - the maximum time to wait for when establishing a connection, in milliseconds
      readTimeout - 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 own OkHttpClient.Builder instances. An example:
                  return new OkHttpClient.Builder()
                              .connectTimeout(5, TimeUnit.MILLISECONDS)
                              .readTimeout(5, TimeUnit.MILLISECONDS)
                              .cookieJar(...);
       
      Specified by:
      getClientBuilder in class ApiConnection
    • logout

      public void logout()
      Forgets the OAuth credentials locally. No requests will be made.
      Specified by:
      logout in class ApiConnection
    • checkCredentials

      public void checkCredentials() throws IOException, MediaWikiApiErrorException
      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 class ApiConnection
      Throws:
      MediaWikiApiErrorException - if the check failed
      IOException
    • getCurrentUser

      public String 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 class ApiConnection
      Returns:
      name of the logged in user
    • getConsumerKey

      public String getConsumerKey()
    • getConsumerSecret

      public String getConsumerSecret()
    • getAccessToken

      public String getAccessToken()
    • getAccessSecret

      public String getAccessSecret()