在Dart中使用Google Firebase和Google API:.dart中是否有以下两个.js程序:
var google = require("googleapis");
// Load the service account key JSON file.
var serviceAccount = require("./xxxxxxxx.json");
// Define the required scopes.
var scopes = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database"
];
// Authenticate a JWT client with the service account.
var jwtClient = new google.auth.JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
scopes
);
// Use the JWT client to generate an access token.
jwtClient.authorize(function(error, tokens) {
if (error) {
console.log("Error making request to generate access token:", error);
} else if (tokens.access_token === null) {
console.log("Provided service account does not have permission to generate access tokens");
} else {
var accessToken = tokens.access_token;
console.log(accessToken);
// See the "Using the access token" section below for information
// on how to use the access token to send authenticated requests to
// the Realtime Database REST API.
}
});
和2。:
在服务器和客户端生成自定义访问令牌(在此处混合使用以进行测试):
var google = require("googleapis");
var serviceAccount = require("xxxxxxxx.json");
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "xxxx"
});
var firebase = require("firebase");
var config = {
apiKey: "xxxx",
authDomain: "xxxxxx",
databaseURL: "xxxx",
projectId: "xxxx",
storageBucket: "xxx",
messagingSenderId: "xxxxx"
};
firebase.initializeApp(config);
var uid = "some-uid";
var additionalClaims = {
premiumAccount: true
};
admin.auth().createCustomToken(uid/*, additionalClaims*/)
.then(function(customToken) {
// begin client use, actually in browser. Here for testing.
firebase.auth().signInWithCustomToken(customToken).then(
function(d){
var fbref = firebase.database().ref('/users/'+uid );
fbref.set({
username: "name",
email: "email",
profile_picture : "imageUrl"
}).catch(function(error) {
console.log("Kann nicht setzen: " + error);
});
var userId = firebase.auth().currentUser.uid;
return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
console.log(snapshot.val());
});
}
).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
});
// end client use
})
.catch(function(error) {
console.log("Error creating custom token:", error);
});
我想知道我需要什么包,以及我可以在哪里获取文档。
至少对飞镖计划的粗略翻译会有所帮助。
答案 0 :(得分:1)
你想要的Dart包在这里:https://pub.dartlang.org/packages/firebase
我们有一些代码可以为服务器端Dart执行此操作:https://github.com/firebase/firebase-dart/blob/master/lib/src/assets/assets_io.dart
你可以用
做类似的事情答案 1 :(得分:1)
要找到一个使用RSA签署令牌的jwt库,RSA似乎是现有库中不太受欢迎的替代品。
pubspec.yaml:
dependencies:
firebase: "4.2.0+1"
just_jwt: "1.3.1"
createCustomToken.dart:
import 'dart:async';
import 'dart:convert' as convert;
import 'package:just_jwt/just_jwt.dart' as jj;
Future main() async {
String client_email = "xxxxxxx";
final DateTime issuedAt = new DateTime.now();
final DateTime expiresAt = issuedAt.add(const Duration(minutes: 30));
String uid = "some-uid";
jj.EncodedJwt encodedJwt;
Map<String, Object> payload = {
"iss": client_email,
"sub": client_email,
"aud": "xxxxxx",
"iat": (issuedAt.millisecondsSinceEpoch / 1000).floor(),
"exp": (issuedAt.millisecondsSinceEpoch / 1000).floor() + (60 * 30),
"uid": uid,
"claims": {}
};
var sign = jj.toTokenSigner(jj.createRS256Signer("xxxxxxxxxx"));
Map<String, jj.TokenSigner> signer = {'RS256': sign};
jj.Encoder encoder = new jj.Encoder(jj.composeTokenSigners(signer));
jj.Jwt jwt = new jj.Jwt.RS256(payload);
encodedJwt = await encoder.convert(jwt);
print(encodedJwt.toString());
}