-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_clarifai_auth_v2.js
148 lines (126 loc) · 4.39 KB
/
test_clarifai_auth_v2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const grpc = require('@grpc/grpc-js');
const { ClarifaiStub } = require('clarifai-nodejs-grpc');
require('dotenv').config();
// Get all potential keys
const clarifaiApiKey = process.env.clarifai_api;
const newClarifaiKey = process.env.new_clarifai_key;
const readApiKey = process.env.read_api;
const writeApiKey = process.env.write_api;
const availableKeys = {
'clarifai_api': clarifaiApiKey,
'new_clarifai_key': newClarifaiKey,
'read_api': readApiKey,
'write_api': writeApiKey
};
// Check if we have any keys to test
const hasKeys = Object.values(availableKeys).some(key => key);
if (!hasKeys) {
console.error('Error: No Clarifai keys found in environment');
process.exit(1);
}
// Initialize the gRPC client
const stub = ClarifaiStub.grpc();
async function testAuth(key, keyName) {
if (!key) {
console.log(`\nSkipping ${keyName} - key not available`);
return false;
}
console.log(`\nTesting ${keyName}:`, key.substring(0, 8) + '...');
// Set up authorization with proper PAT format
const metadata = new grpc.Metadata();
metadata.set('authorization', `Key ${key}`);
try {
console.log('Listing apps to get user and app information...');
const appsResponse = await new Promise((resolve, reject) => {
stub.ListApps(
{
page: 1,
per_page: 1
},
metadata,
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
console.log('Apps response:', JSON.stringify(appsResponse, null, 2));
if (!appsResponse.apps || appsResponse.apps.length === 0) {
console.log(`No apps found using ${keyName}`);
return false;
}
const app = appsResponse.apps[0];
const retrievedUserId = app.user_id;
const retrievedAppId = app.id;
console.log(`Retrieved user_id: ${retrievedUserId}, app_id: ${retrievedAppId}`);
// Test model creation with proper structure
const modelRequest = {
user_app_id: {
user_id: retrievedUserId,
app_id: retrievedAppId
},
model: {
id: 'test-model-' + Date.now(),
name: 'Test Model',
model_type_id: 'visual-classifier',
output_info: {
data: {
concepts: [
{ id: 'cat', name: 'cat' },
{ id: 'dog', name: 'dog' }
]
},
output_config: {
concepts_mutually_exclusive: true,
closed_environment: true
}
}
}
};
const createResponse = await new Promise((resolve, reject) => {
stub.PostModels(modelRequest, metadata, (err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
console.log('Model creation response:', JSON.stringify(createResponse, null, 2));
// If successful, update the .env file with the working key and IDs
if (createResponse.status?.code === 10000) {
const fs = require('fs');
const envContent = `CLARIFAI_API_KEY=${key}
CLARIFAI_USER_ID=${retrievedUserId}
CLARIFAI_APP_ID=${retrievedAppId}
clarifai_api=${key}
READ_API=${process.env.read_api}
WRITE_API=${process.env.write_api}`;
fs.writeFileSync('.env', envContent);
console.log('\nEnvironment variables updated successfully!');
return true;
}
return false;
} catch (error) {
console.error(`Error testing ${keyName}:`, error);
return false;
}
}
async function main() {
let foundValidKey = false;
// Test all available keys
for (const [keyName, key] of Object.entries(availableKeys)) {
const result = await testAuth(key, keyName);
if (result) {
foundValidKey = true;
break;
}
}
if (!foundValidKey) {
console.log('\nNone of the available keys worked as a PAT. A proper PAT is required.');
}
}
main();