Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You can obtain an access token by providing the resource owner's username and password as an authorization grant. It requires the base64 encoded string of the consumer-key:consumer-secret combination. You need to meet the following prerequisites before using the Token API to generate a token. 

Prerequisites

  • A valid user account in the API Store.
  • A valid consumer key and consumer secret pair. Initially, these keys must be generated through the API Store by clicking the Generate link on My Subscriptions page.

Invoking the Token API to generate tokens 
Anchor
GenerateToken
GenerateToken
  

  1. Combine the consumer key and consumer secret keys in the format consumer-key:consumer-secret and encode the combined string using base64. Encoding to base64 can be done using the URL:http://base64encode.org. 
    Here's an example consumer key and secret combination : wU62DjlyDBnq87GlBwplfqvmAbAa:ksdSdoefDDP7wpaElfqvmjDue.
  2. Access the Token API by using a REST client such as cURL, with the following parameters.
    • The token API URL is https://api.cloud.wso2.com:8243/token.
    • payload - "grant_type=password&username=<username>&password=<password>&scope=<scope>". Replace the <username> and <password> values as appropriate.

      Tip

      Tip: <scope> is optional.

      You define scopes for your API's resources so that the resource can only be accessed through a token that has been issued for at least the scope belonging to the resource. For example, if a resource has a scope named 'update' and if the token is issued for the scopes 'read' and 'update', then the token is allowed to access the resource. If the token is issued for 'read' only, the request bearing the particular token will be blocked.

    • headers - Authorization: Basic <base64 encoded string>, Content-Type: application/x-www-form-urlencoded. Replace the <base64 encoded string> as appropriate.          

    For example, use the following cURL command to access the Token API. It generates two tokens as an access token and a refresh token. You can use the refresh token at the time a token is renewed.

    Code Block
    curl -k -d "grant_type=password&username=<username>&password=<password>" -H "Authorization: Basic SVpzSWk2SERiQjVlOFZLZFpBblVpX2ZaM2Y4YTpHbTBiSjZvV1Y4ZkM1T1FMTGxDNmpzbEFDVzhh, Content-Type: application/x-www-form-urlencoded" https://api.cloud.wso2.com:8243/token
    Info
    iconfalse

    User access tokens have a fixed expiration time, which is set to 60 minutes by default. When a user access token expires, the user can try regenerating the token as explained in the Renew user tokens section.

...