[英]How to feed a String to a Google Cloud Function as input?
I need a Google Cloud Function that I can call from my Android app.我需要一个可以从我的 Android 应用程序调用的 Google Cloud Function。 The function should call Vision API Safe Search and return the results to the app.
function 应调用 Vision API Safe Search 并将结果返回到应用程序。
My problem: The function I have is simple and it works.我的问题:我的 function 很简单,而且可以用。 But ONLY from the Cloud Test-Console.
但仅来自云测试控制台。 Not from the android app.
不是来自 android 应用程序。 When I call the function from my app, with the exact same JSON as input, it says
FirebaseFunctionsException: INVALID_ARGUMENT
当我从我的应用程序调用 function 时,使用完全相同的 JSON 作为输入,它说
FirebaseFunctionsException: INVALID_ARGUMENT
Function : Function :
exports.starte = async (req, res) => {
try {
let pfad = req.body.pfad;
console.log('Input is: ' + pfad);
// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
const visionRequest = {
features: [
{ type: 'SAFE_SEARCH_DETECTION' },
],
image: { source: { imageUri: pfad } },
};
const [result] = await client.annotateImage(visionRequest);
const detections = result.safeSearchAnnotation;
console.log('Ergebnis: ' + result);
console.log('Ergebnis: ' + detections);
res.status(200).send(detections);
} catch(err) {
console.log('Error: ' + err.message);
const errorJSON = { message: 'unknown' };
res.status(400).send(errorJSON);
}
};
Android code : Android 代码:
Map<String, Object> data = new HashMap<>();
data.put("pfad", path);
FirebaseFunctions.getInstance()
.getHttpsCallable("nsfw_test2")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
try {
Object result = task.getResult().getData();
Log.v(TAG, "CloudFunktion: Worked!");
return result.toString();
} catch (Exception e) {
e.printStackTrace();
Log.v(TAG, "CloudFunktion: Failed!");
return e.getMessage();
}
}
});
Example JSON input that works in the Cloud Console, but not in Android: {"pfad":"gs:\/\/xxx.appspot.com\/photos\/xxx"}
示例 JSON 输入在 Cloud Console 中有效,但在 Android 中无效:
{"pfad":"gs:\/\/xxx.appspot.com\/photos\/xxx"}
Error I get in Android:我在 Android 中遇到的错误:
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.functions.FirebaseFunctionsException: INVALID_ARGUMENT
at com.google.android.gms.tasks.zzw.getResult(com.google.android.gms:play-services-tasks@@18.0.1:3)
When I check the logs in Google Cloud, it logs: Input is: undefined
(When running from the app)当我检查 Google Cloud 中的日志时,它会记录:
Input is: undefined
(从应用程序运行时)
When I run it from the Cloud Console instead, it works and prints the path I passed.当我改为从 Cloud Console 运行它时,它会工作并打印我通过的路径。
I have absolutely no idea why this happens.我完全不知道为什么会这样。 I have now checked easily 30 websites and stackoverflow questions on this.
我现在已经轻松检查了 30 个网站和 stackoverflow 问题。 To me it seems that my Android code is correct.
对我来说,我的 Android 代码似乎是正确的。
But why doesn't the function read the input then?但是为什么 function 不读取输入呢? Why is the input
undefined
?为什么输入
undefined
? The input in the android app is not null
, I log the input before I pass it in, and it is the correct file. android 应用程序中的输入不是
null
,我在传入之前记录了输入,这是正确的文件。
According to this document , the invalid argument is because we failed to specify an argument correctly:根据这个文档,无效的参数是因为我们没有正确指定一个参数:
public static final FirebaseFunctionsException.Code INVALID_ARGUMENT
公共 static 最终 FirebaseFunctionsException.Code INVALID_ARGUMENT
Client specified an invalid argument.
客户指定了一个无效参数。 Note that this differs from FAILED_PRECONDITION.
请注意,这与 FAILED_PRECONDITION 不同。 INVALID_ARGUMENT indicates arguments that are problematic regardless of the state of the system (eg, an invalid field name).
INVALID_ARGUMENT 表示 arguments 是有问题的,与系统的 state 无关(例如,无效的字段名称)。
You need to create callable functions, instead of using an HTTP triggered Cloud Function.您需要创建可调用函数,而不是使用 HTTP 触发的 Cloud Function。
The Callable functions require to have the following format: Callable 函数需要具有以下格式:
exports.addMessage = functions.https.onCall((data, context) => {
// ...
});
It's not the same as an HTTP triggered Cloud Function (which is what you are using).它与HTTP 触发的 Cloud Function (您正在使用的)不同。 You can still access it if you use an HTTP request from the Android app, but you need to use a Callable Cloud Function.
如果您使用来自 Android 应用程序的 HTTP 请求,您仍然可以访问它,但您需要使用 Callable Cloud Function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.