我正试图使用Swift访问Firebase函数twilioToken返回的令牌,就像我对Redux所做的那样。我附加了用于JS的代码,这样我就可以用Swift来模拟它,但不知道如何从firebase函数调用中访问result.token。我是不是漏掉了什么?我是否需要以不同的方式从https请求中获取值,还是与当前代码接近?如果我需要详细的话请告诉我,谢谢!
output.token上的当前错误是Value of tuple type 'Void' has no member 'token'。
尝试Swift代码:
import UIKit
import MBProgressHUD
import FirebaseFunctions
class CallRoomVC: UIViewController {
private var appDelegate: AppDelegate!
private var userSession: UserSession = FirebaseUserSession.shared
lazy var functions = Functions.functions()
override func viewDidLoad() {
super.viewDidLoad()
appDelegate = UIApplication.shared.delegate as? AppDelegate
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let user = userSession.user else {
fatalError("User instance must be created")
}
var output = functions.httpsCallable("twilioToken").call(["uid": user.id]) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
}
// ...
// or
// print("Result: \(result.token)")
}
}
if (output != nil) {
print("Result: \(output.token)")
}
}
}Firebase函数:
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// eslint-disable-next-line import/no-extraneous-dependencies
const google_cloud_logging = require("@google-cloud/logging");
const twilio = require("twilio");
const cors = require("cors")({
origin: true
});
// eslint-disable-next-line import/no-extraneous-dependencies
admin.initializeApp(functions.config().firebase);
exports.twilioToken = functions.https.onRequest((req, res) => {
return cors(req, res, () => {
const token = new twilio.jwt.AccessToken(
"xxxxxxxxxxxxxx", // Account ID
"xxxxxxxxxxxxxx", // API Key SID
"xxxxxxxxxxxxxx" // API Key Secret
);
token.identity = req.query.uid;
token.addGrant(new twilio.jwt.AccessToken.VideoGrant());
console.log("Sending token: ", token);
res.status(200).send({ token: token.toJwt() });
});
});JS代码:
function* getTokenSaga(action) {
const token = yield call(
rsf.functions.call,
"twilioToken",
{
uid: action.uid
},
{
method: "GET"
}
);
yield put(retrievedToken(token.token));
}
export function* twilioRootSaga() {
yield all([takeEvery(types.TOKEN.GET, getTokenSaga)]);
}发布于 2019-10-02 20:28:47
菲尔·纳什对Swift方面的解释起了作用,但问题在于我的Firebase函数,我必须根据Twilio/Firebase函数API文档为它创建一个新函数:
exports.twilioTokenV2 = functions.https.onCall((data, context) => {
const AccessToken = twilio.jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const twilioAccountSid = functions.config().twilio_api.account_sid;
const twilioApiKey = functions.config().twilio_api.key;
const twilioApiSecret = functions.config().twilio_api.secret;
// Grab uid passed in for identity
const identity = data.uid;
// Grab question ID passed in for room name
const videoGrant = new VideoGrant({
room: data.qid,
});
// Create an access token which we will sign and return to the client,
// containing the grant we just created
const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
token.addGrant(videoGrant);
token.identity = identity;
console.log("Sending token: ", token);
return {
token: token.toJwt()
}
});发布于 2019-09-23 03:28:12
两位开发人员在这里传道。
对Firebase函数的调用是异步的,因为它正在发出HTTP请求。结果不会返回给您的output变量,但是它可以作为result对象在回调中使用。您需要使用这个result,下面是这样的内容:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let user = userSession.user else {
fatalError("User instance must be created")
}
functions.httpsCallable("twilioToken").call(["uid": user.id]) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
}
}
if let token = (result?.data as? [String: Any])?["token"] as? String {
print("Result: \(token)")
}
}
}这个例子是从这里的Firebase文档改编的。
如果有帮助的话请告诉我。
https://stackoverflow.com/questions/58018002
复制相似问题