簡體   English   中英

如何將字符串作為輸入提供給 Google Cloud Function?

[英]How to feed a String to a Google Cloud Function as input?

我需要一個可以從我的 Android 應用程序調用的 Google Cloud Function。 function 應調用 Vision API Safe Search 並將結果返回到應用程序。

我的問題:我的 function 很簡單,而且可以用。 但僅來自雲測試控制台。 不是來自 android 應用程序。 當我從我的應用程序調用 function 時,使用完全相同的 JSON 作為輸入,它說FirebaseFunctionsException: INVALID_ARGUMENT

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 代碼

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();
                }
            }
        });

示例 JSON 輸入在 Cloud Console 中有效,但在 Android 中無效: {"pfad":"gs:\/\/xxx.appspot.com\/photos\/xxx"}

我在 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)

當我檢查 Google Cloud 中的日志時,它會記錄: Input is: undefined (從應用程序運行時)

當我改為從 Cloud Console 運行它時,它會工作並打印我通過的路徑。


我完全不知道為什么會這樣。 我現在已經輕松檢查了 30 個網站和 stackoverflow 問題。 對我來說,我的 Android 代碼似乎是正確的。

但是為什么 function 不讀取輸入呢? 為什么輸入undefined android 應用程序中的輸入不是null ,我在傳入之前記錄了輸入,這是正確的文件。

根據這個文檔,無效的參數是因為我們沒有正確指定一個參數:

公共 static 最終 FirebaseFunctionsException.Code INVALID_ARGUMENT

客戶指定了一個無效參數。 請注意,這與 FAILED_PRECONDITION 不同。 INVALID_ARGUMENT 表示 arguments 是有問題的,與系統的 state 無關(例如,無效的字段名稱)。

您需要創建可調用函數,而不是使用 HTTP 觸發的 Cloud Function。

Callable 函數需要具有以下格式:

exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

它與HTTP 觸發的 Cloud Function (您正在使用的)不同。 如果您使用來自 Android 應用程序的 HTTP 請求,您仍然可以訪問它,但您需要使用 Callable Cloud Function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM