Skip to content

Instantly share code, notes, and snippets.

@musistudio
Last active April 5, 2026 14:00
Show Gist options
  • Select an option

  • Save musistudio/f5a67841ced39912fd99e42200d5ca8b to your computer and use it in GitHub Desktop.

Select an option

Save musistudio/f5a67841ced39912fd99e42200d5ca8b to your computer and use it in GitHub Desktop.
qwen-cli.js
const os = require("os");
const path = require("path");
const fs = require("fs/promises");
const OAUTH_FILE = path.join(os.homedir(), ".qwen", "oauth_creds.json");
class QwenCLITransformer {
name = "qwen-cli";
async transformRequestIn(request, provider) {
if (!this.oauth_creds) {
await this.getOauthCreds();
}
if (this.oauth_creds && this.oauth_creds.expiry_date < +new Date()) {
await this.refreshToken(this.oauth_creds.refresh_token);
}
if (request.stream) {
request.stream_options = {
include_usage: true,
};
}
return {
body: request,
config: {
headers: {
Authorization: `Bearer ${this.oauth_creds.access_token}`,
"User-Agent": "QwenCode/v22.12.0 (darwin; arm64)",
},
},
};
}
refreshToken(refresh_token) {
const urlencoded = new URLSearchParams();
urlencoded.append("client_id", "f0304373b74a44d2b584a3fb70ca9e56");
urlencoded.append("refresh_token", refresh_token);
urlencoded.append("grant_type", "refresh_token");
return fetch("https://chat.qwen.ai/api/v1/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: urlencoded,
})
.then((response) => response.json())
.then(async (data) => {
data.expiry_date =
new Date().getTime() + data.expires_in * 1000 - 1000 * 60;
data.refresh_token = refresh_token;
delete data.expires_in;
this.oauth_creds = data;
await fs.writeFile(OAUTH_FILE, JSON.stringify(data, null, 2));
});
}
async getOauthCreds() {
try {
const data = await fs.readFile(OAUTH_FILE);
this.oauth_creds = JSON.parse(data);
} catch (e) {}
}
}
module.exports = QwenCLITransformer;
@andromedarabbit
Copy link
Copy Markdown

not working at all

@oniharnantyo
Copy link
Copy Markdown

Still works on my device but sometime the token is expired and need to be refreshed using this steps:

  • Open qwen code and authenticate using OAUTH2
  • Close the qwen code then execute ccr restart
  • Continue using claude code that previously opened with ccr code

@dvelm
Copy link
Copy Markdown

dvelm commented Mar 23, 2026

any ways to use now ?

@mike2003
Copy link
Copy Markdown

mike2003 commented Apr 5, 2026

how use this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment