简体   繁体   English

如何将字符串作为输入提供给 Google Cloud Function?

[英]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.

相关问题 谷歌云 Function:如何继续超时 function - Google Cloud Function : how to continues timeout function 如何将 Google Cloud 凭据导入 Firebase Cloud Function? - How to import Google Cloud credentials into a Firebase Cloud Function? Google Cloud PubSub - 如何将多个 arguments 发送到云端 Function - Google Cloud PubSub - How to send multiple arguments to the Cloud Function 如何使用 Cloud Function 服务帐户在 Cloud Function 中创建签名的 Google Cloud Storage URL? - How to create signed Google Cloud Storage URLs in Cloud Function using Cloud Function service account? 如何为 HTTP 触发器验证谷歌云 function? - How to authenticate google cloud function for HTTP trigger? 如何在没有云的情况下使用 Google PubSub Function - How to use Google PubSub without Cloud Function 如何使用 Firebase Cloud Function 加密字符串 - How to encrypt a String with a Firebase Cloud Function 如何在特定文件集从谷歌云到达云存储时启动云数据流管道 function - how to launch a cloud dataflow pipeline when particular set of files reaches Cloud storage from a google cloud function 测试谷歌云 Function - Test Google Cloud Function 谷歌云 Function 固定链接 - Google Cloud Function Permalink
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM