Typescript 如何从Firebase云函数中调用Google Places API?
我正试图在我的Flitter应用程序中,借助GooglePlacesAPI及其自动完成功能实现一个位置自动完成功能。为了使API调用逻辑和API密钥远离客户端,Flatter应用程序应该调用Firebase云函数,然后Firebase云函数调用Google Places API。几天来,我一直在努力让它发挥作用,阅读了各种各样的文章,但我无法取得任何进展。这是我的Firebase云函数的源代码:Typescript 如何从Firebase云函数中调用Google Places API?,typescript,flutter,autocomplete,google-cloud-functions,google-places-api,Typescript,Flutter,Autocomplete,Google Cloud Functions,Google Places Api,我正试图在我的Flitter应用程序中,借助GooglePlacesAPI及其自动完成功能实现一个位置自动完成功能。为了使API调用逻辑和API密钥远离客户端,Flatter应用程序应该调用Firebase云函数,然后Firebase云函数调用Google Places API。几天来,我一直在努力让它发挥作用,阅读了各种各样的文章,但我无法取得任何进展。这是我的Firebase云函数的源代码: import * as functions from "firebase-function
import * as functions from "firebase-functions";
import * as axios from "axios";
export const autofillSuggestions = functions.region("europe-west3")
.https.onCall(async (data) => {
const input = data.input;
const sessionToken = data.sessiontoken;
// will be stored as an environment variable, as soon as i get cloud function to work
const googleAPIKey = "my_api_key";
const requestURL = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + input +
"&key=" + googleAPIKey + "&sessiontoken=" + sessionToken;
return await axios.default.get(requestURL).then((apiResponse) => {
return {predictions: apiResponse.data.predictions,
};
})
.catch((error) => {
console.log(error);
});
});
此源代码被导入到index.ts
中,然后再次导出该文件,因此问题不应存在于此。
Flatter应用程序中的云函数调用逻辑如下所示:
HttpsCallable _getCallable(String functionName) =>
FirebaseFunctions.instance.httpsCallable(functionName);
Future<dynamic> _getSuggestionsFromGoogleMapsAPI(String input) async {
//FirebaseFunctions.instance
// .useFunctionsEmulator(origin: 'http://localhost:5001');
final callable = _getCallable('autofillSuggestions');
try {
final data = HashMap.of({
'sugg': input,
'sessionToken': _sessionToken,
});
final response = await callable.call(data);
return response.data;
} catch (e) {
print(e);
throw AutocompletionFailure();
}
}
autofillSuggestions
{
"@type":"type.googleapis.com/google.cloud.audit.AuditLog",
"authenticationInfo":{"principalEmail":"my@mail.com"},
"requestMetadata":
{
"callerIp":"10.10.101.101",
"callerSuppliedUserAgent":"FirebaseCLI/9.8.0,gzip(gfe),gzip(gfe)",
"requestAttributes":{"time":"2021-04-04T09:34:15.120516Z","auth":{}},
"destinationAttributes":{}
},
"serviceName":"cloudfunctions.googleapis.com",
"methodName":"google.cloud.functions.v1.CloudFunctionsService.SetIamPolicy",
"authorizationInfo":
[
{
"resource":"projects/my-project/locations/europe-west3/functions/autofillSuggestions",
"permission":"cloudfunctions.functions.setIamPolicy",
"granted":true,
"resourceAttributes":{}
},
{
"permission":"cloudfunctions.functions.setIamPolicy",
"granted":true,
"resourceAttributes":{}
}
],
"resourceName":"projects/my-project/locations/europe-west3/functions/autofillSuggestions",
"request":
{
"updateMask":"version,bindings",
"resource":"projects/my-project/locations/europe-west3/functions/autofillSuggestions",
"policy":
{
"bindings":
[
{
"members":["allUsers"],
"role":"roles/cloudfunctions.invoker"
}
]
},
"@type":"type.googleapis.com/google.iam.v1.SetIamPolicyRequest"
},
"response":
{
"bindings":
[
{
"members":["allUsers"],
"role":"roles/cloudfunctions.invoker"
}
],
"@type":"type.googleapis.com/google.iam.v1.Policy","etag":"BwW/IkjRmog="
},
"resourceLocation":{"currentLocations":["europe-west3"]}
}
因为它没有提到类似于函数执行已开始
或函数执行已终止的任何内容代码>我怀疑云函数根本没有被调用。。。我的想法对吗?如果是,有人知道如何让它工作吗
我把自己的注意力集中在和上。我也发现很有趣,但还没有尝试过,因为我认为我的主要问题是云函数甚至没有被调用
非常感谢您的帮助!大家复活节快乐 答案是——当然——相当愚蠢,但我想这是它的诅咒和自动欺骗。。。
如您所见,由于我的位置,我指定了europe-west3
作为我的云功能区域。但是在获取callable
引用时,我没有指定区域,这导致cloud\u functions
flatter包创建一个FirebaseFunctions
实例,默认区域设置为us-central1
。这打乱了通话,导致内部错误。Fatter应用程序中提供了简易修复:
\u getCallable
的新实现:
HttpsCallable _getCallable(String functionName) {
return FirebaseFunctions.instanceFor(region: 'europe-west3')
.httpsCallable(functionName);
}
就这样!现在我可以享受我的复活节午餐了!节日快乐