Skip to main content

OAuth Authorization

Obtain, refresh, and revoke QwenWork user access tokens using Authorization Code and PKCE.

OAuth lets your app obtain an access token with user consent and call OpenAPI resources the user can access.

Endpoints and prerequisites

Use https://open.qwenwork.ai for authorization and https://openapi.qwenwork.ai for token exchange, refresh, UserInfo, and revocation. Append the following paths to the corresponding domain:
PurposeMethodPath
Start authorizationGET/oauth2/auth
Obtain or refresh a tokenPOST/oapi/v1/oauth2/accessToken
Query the authorized userGET/oapi/v1/oauth2/userinfo
Revoke authorizationPOST/oapi/v1/oauth2/revoke
Complete app creation and publication and prepare the App ID, app secret, callback URL, and enabled business scopes. The flow uses PKCE (S256), with token exchange performed on your server.

1. Generate state and PKCE parameters

Generate a fresh random state and code_verifier for each authorization request. Store them securely in your server-side session and bind them to the same browser authorization flow. Generate code_challenge using SHA-256 and Base64URL encoding without padding:
import { randomBytes, createHash } from 'node:crypto';

const state = randomBytes(32).toString('base64url');
const codeVerifier = randomBytes(32).toString('base64url');
const codeChallenge = createHash('sha256')
  .update(codeVerifier)
  .digest('base64url');
// Store state and codeVerifier securely in the current session.

2. Redirect to the authorization page

ParameterDescription
response_typeSet to code
client_idApp ID
redirect_uriCallback URL in the app’s published configuration
scopeSpace-separated business scopes; explicitly request the permissions needed for this flow
stateRandom value from the previous step, checked on callback
code_challengePKCE challenge from the previous step
code_challenge_methodSet to S256
Build the authorization URL in the same server-side flow. This example explicitly requests file read permission:
const url = new URL('https://open.qwenwork.ai/oauth2/auth');
url.search = new URLSearchParams({
  response_type: 'code',
  client_id: process.env.QW_APP_ID,
  redirect_uri: 'https://your-app.example.com/oauth/callback',
  scope: 'drive.file.read openid offline_access',
  state,
  code_challenge: codeChallenge,
  code_challenge_method: 'S256',
}).toString();
// Redirect the current user to url.toString() using an HTTP redirect.

The user reviews the permissions and agrees to continue

Permissions and consent button on the user authorization page
Set scope to the required, enabled business permissions and add openid offline_access for identity and refresh capabilities. Specifying only these two protocol scopes, or omitting scope, requests all available business permissions for the app. Users signed in with an enterprise identity use organization authorization. Users signed in with a personal identity use personal authorization.

Callback URL requirements

  • Enter a complete HTTPS URL with an exact hostname and path.
  • Configure up to 20 unique URLs, each up to 2048 Unicode characters.
  • Local development supports HTTP on localhost, 127.0.0.0/8, or ::1.
  • Use the same redirect_uri for authorization and token exchange, matching the published configuration.
  • Publish the app after changing callback URLs to apply the changes.

3. Handle the callback and exchange the code

After consent, the callback receives code and state. Confirm that state matches the value stored in the session, then exchange the single-use authorization code for tokens. Send JSON or application/x-www-form-urlencoded from your server. This guide uses JSON:
curl --request POST \
  'https://openapi.qwenwork.ai/oapi/v1/oauth2/accessToken' \
  --header 'Content-Type: application/json' \
  --data '{
    "grant_type": "authorization_code",
    "app_id": "YOUR_APP_ID",
    "app_secret": "YOUR_APP_SECRET",
    "code": "CODE_FROM_CALLBACK",
    "redirect_uri": "https://your-app.example.com/oauth/callback",
    "code_verifier": "VERIFIER_SAVED_FOR_THIS_REQUEST"
  }'
Use the original code_verifier saved in step 1. Send all token exchange parameters in the request body, with each field appearing once.

Read the token response

The successful JSON response contains:
FieldDescription
access_tokenUser token for protected resources; store as an opaque string
token_typeBearer
token_useopen_app_user for a user token
expires_inAccess token lifetime in seconds, as returned by the response
scopeOptional; permissions actually granted
refresh_tokenOptional; refresh credential to store securely
id_tokenOptional; identity token
Store the complete access_token string for subsequent API requests.

4. Call user resources

This example queries a file and requires drive.file.read:
curl --get 'https://openapi.qwenwork.ai/v1/drive/files' \
  --header "Authorization: Bearer $QW_ACCESS_TOKEN" \
  --data-urlencode 'path=/example.txt'
Replace the placeholder path with a file owned by the user. See Get File Information and Download URL. Use UserInfo to confirm the authorized identity:
curl 'https://openapi.qwenwork.ai/oapi/v1/oauth2/userinfo' \
  --header "Authorization: Bearer $QW_ACCESS_TOKEN"
UserInfo returns the user identifier sub. Organization authorization also returns org_id, and user.profile.read permission adds name.

5. Refresh tokens

curl --request POST \
  'https://openapi.qwenwork.ai/oapi/v1/oauth2/accessToken' \
  --header 'Content-Type: application/json' \
  --data '{
    "grant_type": "refresh_token",
    "app_id": "YOUR_APP_ID",
    "app_secret": "YOUR_APP_SECRET",
    "refresh_token": "SAVED_REFRESH_TOKEN"
  }'
Save the new access token and refresh token after a successful refresh. Refresh serially for the same authorization, using the latest refresh token each time.

6. Revoke authorization

curl --request POST \
  'https://openapi.qwenwork.ai/oapi/v1/oauth2/revoke' \
  --header 'Content-Type: application/json' \
  --data '{
    "app_id": "YOUR_APP_ID",
    "app_secret": "YOUR_APP_SECRET",
    "token": "TOKEN_TO_REVOKE",
    "token_type_hint": "refresh_token"
  }'
The optional token_type_hint is access_token or refresh_token. Successful revocation returns an empty HTTP 200 and invalidates the app’s business authorization for that user. Pause or delete existing scheduled tasks separately.

Common errors

Token errors contain error, error_description, and error_code. If the user cancels authorization, the callback returns error=access_denied.
error_codeWhat to do
invalid_request_parametersCheck required fields, duplicate keys, mixed credential fields, and parameter locations
invalid_client_credentialsCheck the App ID, secret, and environment
invalid_codeCheck code validity and matching callback and PKCE values; authorize again if needed
refresh_token_not_activeCheck for an old or inactive refresh token; authorize again if needed
authorization_revokedObtain user authorization again
app_scope_changedCheck app permissions and authorize again
dependency_unavailableKeep the request details and contact the platform team
If authorization succeeds but a business API returns 403, check its scope, the authorized identity, and resource ownership. See the OpenAPI Overview.