Node.js 为什么可以';Clarifai是否使用Clarifai门户中生成的API密钥或个人访问令牌验证模型输出请求?
更新 我能够获得我的原始代码,并且在隔离运行时,这些建议也能正常工作。但是,我需要做的是从Firebase的Node.js 为什么可以';Clarifai是否使用Clarifai门户中生成的API密钥或个人访问令牌验证模型输出请求?,node.js,firebase,authorization,malformed,clarifai,Node.js,Firebase,Authorization,Malformed,Clarifai,更新 我能够获得我的原始代码,并且在隔离运行时,这些建议也能正常工作。但是,我需要做的是从Firebase的onRequest或onCall函数中调用它。当这段代码被这些包住时,格式错误的头和授权请求仍然是一个问题。我们以这种方式使用了许多其他API,因此让人费解的是,为什么澄清API会出现这些问题。对Firebase使用它有什么建议吗 原创 Clarifai新手,在尝试从食物模型检索模型输出时遇到一些身份验证问题 我试过两个不同的键: 从我在门户中创建的应用程序生成的API密钥 API密钥-
onRequest
或onCall
函数中调用它。当这段代码被这些包住时,格式错误的头和授权请求仍然是一个问题。我们以这种方式使用了许多其他API,因此让人费解的是,为什么澄清API会出现这些问题。对Firebase使用它有什么建议吗
原创
Clarifai新手,在尝试从食物模型检索模型输出时遇到一些身份验证问题
我试过两个不同的键:
- 从我在门户中创建的应用程序生成的API密钥
- API密钥-我为自己生成的个人访问令牌
空的或格式错误的授权标头响应
{
"status":{
"code":11102,
"description":"Invalid request",
"details":"Empty or malformed authorization header. Please provide an API key or session token.",
"req_id":"xyzreasdfasdfasdfasdfasf"
},
"outputs":[
]
}
我在下面的文章中拼凑了这段代码。这是在Node 10环境中运行的
确保您遵守了在代码中传递密钥的格式,如下所示:
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key {YOUR_CLARIFAI_API_KEY}");
确保“钥匙”存在
让我知道。确保您遵守了在代码中传递密钥的格式:
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key {YOUR_CLARIFAI_API_KEY}");
确保“钥匙”存在
让我知道。更新:7.0.2之前的版本中存在一个问题,如果您有另一个库使用不同版本的@grpc/grpc js
,则grpc.Metadata
对象不一定是从clarifai grpc nodejs
使用的库版本构建的
要解决此问题,请更新clarifai grpc nodejs
库,并要求grpc
对象如下:
{
const { ClarifaiStub } = require('clarifai-nodejs-grpc');
const grpc = require('@grpc/grpc-js');
const stub = ClarifaiStub.json();
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key {Personal Access Token}"); // Sounds like you've made the personal access token correctly - go into settings, then authentication, then create one. Make sure it has proper permissions (I believe all by default).
return new Promise((resolve, reject) => {
stub.PostModelOutputs(
{
user_app_id: {
user_id: "{USER ID}", // I used my actual ID, I did not put 'me'. You can find this under your profile.
app_id: "{APP NAME}" // This is the app ID found in the upper left corner of the app after it is created - not the API Key. This is generally what you named the app when you created it.
},
model_id: 'bd367be194cf45149e75f01d59f77ba7',
inputs: [{ data: { image: { url: 'https://samples.clarifai.com/metro-north.jpg' } } }],
},
metadata,
(err, response) => {
if (err) {
return reject(`ERROR: ${err}`);
}
console.log(JSON.stringify(response));
resolve(JSON.stringify(response));
}
);
});
}
const{ClarifaiStub,grpc}=require(“clarifai nodejs grpc”);
以前,grpc
对象是直接从@grpc/grpc js
导入的,这是问题的根源
有两种方法可以对Clarifai API进行身份验证:
- 使用特定于应用程序的API密钥,这意味着API密钥附加到应用程序,并且只能在该应用程序内执行操作
- 使用特定于用户的个人访问令牌(PAT),这意味着您可以对用户拥有/有权访问的所有应用程序进行评估/操作/操作(以及创建/更新/删除应用程序本身)
使用PAT时,必须在请求数据中指定目标应用程序。对于API密钥,这是不需要的
我已经用一个有效的API密钥测试了您的示例(使用节点12,尽管它也应该在10中工作),并且它可以在fina中工作(在将其放入异步函数之后)。下面是一个完整的可运行示例(用有效的API密钥替换您的\u API\u密钥
)
如果您想在上面的示例中使用PAT,则必须更改两件事。首先,用PAT替换API密钥:
...
metadata.set("authorization", "Key YOUR_PAT");
...
向方法请求对象添加应用程序ID
...
stub.PostModelOutputs(
{
user_app_id: {
user_id: "me", // The literal "me" resolves to your user ID.
app_id: "YOUR_APPLICATION_ID"
},
model_id: 'bd367be194cf45149e75f01d59f77ba7',
inputs: [{ data: { image: { url: 'https://samples.clarifai.com/metro-north.jpg' } } }],
},
...
更新:7.0.2之前的版本中存在一个问题,如果您有另一个库使用不同版本的@grpc/grpc js
,则grpc.Metadata
对象不一定是从clarifai grpc nodejs
使用的库版本构建的
要解决此问题,请更新clarifai grpc nodejs
库,并要求grpc
对象如下:
{
const { ClarifaiStub } = require('clarifai-nodejs-grpc');
const grpc = require('@grpc/grpc-js');
const stub = ClarifaiStub.json();
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key {Personal Access Token}"); // Sounds like you've made the personal access token correctly - go into settings, then authentication, then create one. Make sure it has proper permissions (I believe all by default).
return new Promise((resolve, reject) => {
stub.PostModelOutputs(
{
user_app_id: {
user_id: "{USER ID}", // I used my actual ID, I did not put 'me'. You can find this under your profile.
app_id: "{APP NAME}" // This is the app ID found in the upper left corner of the app after it is created - not the API Key. This is generally what you named the app when you created it.
},
model_id: 'bd367be194cf45149e75f01d59f77ba7',
inputs: [{ data: { image: { url: 'https://samples.clarifai.com/metro-north.jpg' } } }],
},
metadata,
(err, response) => {
if (err) {
return reject(`ERROR: ${err}`);
}
console.log(JSON.stringify(response));
resolve(JSON.stringify(response));
}
);
});
}
const{ClarifaiStub,grpc}=require(“clarifai nodejs grpc”);
以前,grpc
对象是直接从@grpc/grpc js
导入的,这是问题的根源
有两种方法可以对Clarifai API进行身份验证:
- 使用特定于应用程序的API密钥,这意味着API密钥附加到应用程序,并且只能在该应用程序内执行操作
- 使用特定于用户的个人访问令牌(PAT),这意味着您可以对用户拥有/有权访问的所有应用程序进行评估/操作/操作(以及创建/更新/删除应用程序本身)
使用PAT时,必须在请求数据中指定目标应用程序。对于API密钥,这是不需要的
我已经用一个有效的API密钥测试了您的示例(使用节点12,尽管它也应该在10中工作),并且它可以在fina中工作(在将其放入异步函数之后)。下面是一个完整的可运行示例(用有效的API密钥替换您的\u API\u密钥
)
如果您想在上面的示例中使用PAT,则必须更改两件事。首先,用PAT替换API密钥:
...
metadata.set("authorization", "Key YOUR_PAT");
...
向方法请求对象添加应用程序ID
...
stub.PostModelOutputs(
{
user_app_id: {
user_id: "me", // The literal "me" resolves to your user ID.
app_id: "YOUR_APPLICATION_ID"
},
model_id: 'bd367be194cf45149e75f01d59f77ba7',
inputs: [{ data: { image: { url: 'https://samples.clarifai.com/metro-north.jpg' } } }],
},
...
编辑:看起来Firebase不支持自定义标题。这可能会影响“授权”标题。至少这是我最好的猜测。请参见以下票据中的注释
以下代码适用于我:
{
const { ClarifaiStub } = require('clarifai-nodejs-grpc');
const grpc = require('@grpc/grpc-js');
const stub = ClarifaiStub.json();
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key {APP API KEY}");
return new Promise((resolve, reject) => {
stub.PostModelOutputs(
{
model_id: 'bd367be194cf45149e75f01d59f77ba7',
inputs: [{ data: { image: { url: 'https://samples.clarifai.com/metro-north.jpg' } } }],
},
metadata,
(err, response) => {
if (err) {
return reject(`ERROR: ${err}`);
}
console.log(JSON.stringify(response));
resolve(JSON.stringify(response));
}
);
});
}
缺少{
,但我不确定这是否反映在您正在运行的实际代码中。在本例中,我使用的是应用程序API键(当您创建应用程序时,应用程序详细信息页面上将有一个API键)
听起来您可能使用的是个人访问令牌,可以这样使用:
{
const { ClarifaiStub } = require('clarifai-nodejs-grpc');
const grpc = require('@grpc/grpc-js');
const stub = ClarifaiStub.json();
const metadata = new grpc.Metadata();
metadata.set("authorization", "Key {Personal Access Token}"); // Sounds like you've made the personal access token correctly - go into settings, then authentication, then create one. Make sure it has proper permissions (I believe all by default).
return new Promise((resolve, reject) => {
stub.PostModelOutputs(
{
user_app_id: {
user_id: "{USER ID}", // I used my actual ID, I did not put 'me'. You can find this under your profile.
app_id: "{APP NAME}" // This is the app ID found in the upper left corner of the app after it is created - not the API Key. This is generally what you named the app when you created it.
},
model_id: 'bd367be194cf45149e75f01d59f77ba7',
inputs: [{ data: { image: { url: 'https://samples.clarifai.com/metro-north.jpg' } } }],
},
metadata,
(err, response) => {
if (err) {
return reject(`ERROR: ${err}`);
}
console.log(JSON.stringify(response));
resolve(JSON.stringify(response));
}
);
});
}
确保填写:{Personal Access Token}、{USER ID}和{APP NAME}。我使用了我的实际用户ID(在配置文件中找到),并且应用程序名称不是应用程序的API密钥,而是位于应用程序详细信息页面左上角的名称。此调用对我有效。编辑:因此Firebase似乎不支持自定义标题。这可能会影响“授权”标题。在