Android发送HTTP POST以使用Retrofit触发云功能

时间:2019-03-14 23:20:26

标签: android firebase http retrofit google-cloud-functions

我有一个使用“ context.auth.uid”的云函数,出于JSON解析的原因,我想通过使用Retrofit代替Firebase函数API来触发它。但是,当我尝试这样做时出现此错误:Request has incorrect Content-Type. app/google-services.json。而且我不知道如何传递功能API传递的身份验证信息。 所以这是我的代码:

云函数HTTP函数:

exports.getMatches = functions.https.onCall((req, context) => {

    var uid = context.auth.uid;
    console.log("uid: ", uid);
});

用于改进的Android SearchAPI接口

public interface ElasticSearchAPI
 {

    @POST("getMatches/")
    Call<ElasticHitsObject> getMatches(@Header("Content-Type") String 
    content_type,@Header("Authorization") String secretKey);

 }

Android调用该函数

   Call<ElasticHitsObject> call = searchAPI.getMatches("app/google-services.json","api_key");

1 个答案:

答案 0 :(得分:1)

如代码中所声明的,Retrofit不知道如何调用callable functions。他们有一个special protocol,旨在与Firebase客户端SDK一起使用。

如果要使用HTTP客户端库来调用Cloud Functions,则应改成HTTP function,而不是可调用。请注意,它们的声明不同。一个以functions.https.onCall()开头,另一个以functions.https.onRequest()开头。