GetStream 添加 activity 由于 403 而失败
GetStream add activity fails due 403
正在为 Swift 尝试 GetStream,我无法添加 activity。
class MyActivity : Activity {}
...
let client = Client(apiKey: <MyApiKey>, appId: <MyApiKey>, token: <Token>)
let ericFeed = client.flatFeed(feedSlug: "user", userId: "eric")
let activity = MyActivity(actor: "eric", verb: "waves", object: "picture:10", foreignId: "picture:10")
ericFeed.add(activity) { result in
print("!result!")
print(result)
}
token
是在服务器端生成的,格式为 eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiZXJpYyJ9.AAAAAAAAAAAAAAAAA-AAAAAAAAAAAAAAAA-AAAAAAAA
并且:
client.currentUserId
returns eric
(所以,令牌是正确的?)
ericFeed.add(activity)
的回调没有被调用
- 在我的应用程序的仪表板日志中,我看到添加 activity 的尝试失败,出现错误 403
我尝试了不同的 ID(使用不同的标记),actor: "eric"
和 actor: "user:eric"
。可能出了什么问题?
生成令牌(php 服务器)的代码是:
$userId = "eric";
$client = new GetStream\Stream\Client(<MyApiKey>, <MyAppSecret>);
$userToken = $client->createUserSessionToken($userId);
我在仪表板上收到日志:
有几件事需要牢记。
首先,可能您的客户端在请求结束时被释放,这就是没有调用回调的原因,但日志可以向您显示请求已完成。我建议你使用一个共享的 Client 实例,它会很容易使用。要设置共享客户端,您需要这样写:
Client.config = .init(apiKey: "<MyApiKey>", appId: "<MyApiKey>", token: "<Token>")
有关 wiki page 中客户端设置的更多信息。
第二个重要的事情,你必须 create/update 你的 Stream 用户。从服务器端,您将获得带有 Stream userId
的令牌,并且可以请求 Stream 用户。最简单的方法是在用户 created/updated 处调用 Client.shared.create(user:)
。因此,它仍然是 Stream Client 设置的一部分:
Client.shared.create(user: GetStream.User(id: Client.shared.currentUserId!)) { result in
// Update the client with the current user.
Client.shared.currentUser = try? result.get()
// Do all your requests from here. Reload feeds and etc.
}
更多信息见docs。
我建议您只使用 feedSlug
参数创建提要,并且 Stream userId
将从令牌中获取。但它是可选的,因为 currentUserId
是可选的。例如:
let ericFeed = Client.shared.flatFeed(feedSlug: "user")
ericFeed?.add(activity)
对于您的活动,Stream 客户端应始终将当前 Stream 用户用作参与者。因此,我们需要更新您的 MyActivity
.
的定义
最后,这是您应该可以使用的代码:
// Define your activity.
class MyActivity: EnrichedActivity<GetStream.User, String, DefaultReaction> {
// ...
}
// Setup Stream Client.
Client.config = .init(apiKey: <MyApiKey>, appId: <MyApiKey>, token: <Token>)
// Setup the current user.
Client.shared.getCurrentUser {
let ericFeed = Client.shared.flatFeed(feedSlug: "user")
let activity = MyActivity(actor: Client.shared.currentUser!, verb: "waves", object: "picture:10", foreignId: "picture:10")
ericFeed?.add(activity) { result in
print("!result!")
print(result)
}
}
正在为 Swift 尝试 GetStream,我无法添加 activity。
class MyActivity : Activity {}
...
let client = Client(apiKey: <MyApiKey>, appId: <MyApiKey>, token: <Token>)
let ericFeed = client.flatFeed(feedSlug: "user", userId: "eric")
let activity = MyActivity(actor: "eric", verb: "waves", object: "picture:10", foreignId: "picture:10")
ericFeed.add(activity) { result in
print("!result!")
print(result)
}
token
是在服务器端生成的,格式为 eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiZXJpYyJ9.AAAAAAAAAAAAAAAAA-AAAAAAAAAAAAAAAA-AAAAAAAA
并且:
client.currentUserId
returnseric
(所以,令牌是正确的?)ericFeed.add(activity)
的回调没有被调用- 在我的应用程序的仪表板日志中,我看到添加 activity 的尝试失败,出现错误 403
我尝试了不同的 ID(使用不同的标记),actor: "eric"
和 actor: "user:eric"
。可能出了什么问题?
生成令牌(php 服务器)的代码是:
$userId = "eric";
$client = new GetStream\Stream\Client(<MyApiKey>, <MyAppSecret>);
$userToken = $client->createUserSessionToken($userId);
我在仪表板上收到日志:
有几件事需要牢记。 首先,可能您的客户端在请求结束时被释放,这就是没有调用回调的原因,但日志可以向您显示请求已完成。我建议你使用一个共享的 Client 实例,它会很容易使用。要设置共享客户端,您需要这样写:
Client.config = .init(apiKey: "<MyApiKey>", appId: "<MyApiKey>", token: "<Token>")
有关 wiki page 中客户端设置的更多信息。
第二个重要的事情,你必须 create/update 你的 Stream 用户。从服务器端,您将获得带有 Stream userId
的令牌,并且可以请求 Stream 用户。最简单的方法是在用户 created/updated 处调用 Client.shared.create(user:)
。因此,它仍然是 Stream Client 设置的一部分:
Client.shared.create(user: GetStream.User(id: Client.shared.currentUserId!)) { result in
// Update the client with the current user.
Client.shared.currentUser = try? result.get()
// Do all your requests from here. Reload feeds and etc.
}
更多信息见docs。
我建议您只使用 feedSlug
参数创建提要,并且 Stream userId
将从令牌中获取。但它是可选的,因为 currentUserId
是可选的。例如:
let ericFeed = Client.shared.flatFeed(feedSlug: "user")
ericFeed?.add(activity)
对于您的活动,Stream 客户端应始终将当前 Stream 用户用作参与者。因此,我们需要更新您的 MyActivity
.
最后,这是您应该可以使用的代码:
// Define your activity.
class MyActivity: EnrichedActivity<GetStream.User, String, DefaultReaction> {
// ...
}
// Setup Stream Client.
Client.config = .init(apiKey: <MyApiKey>, appId: <MyApiKey>, token: <Token>)
// Setup the current user.
Client.shared.getCurrentUser {
let ericFeed = Client.shared.flatFeed(feedSlug: "user")
let activity = MyActivity(actor: Client.shared.currentUser!, verb: "waves", object: "picture:10", foreignId: "picture:10")
ericFeed?.add(activity) { result in
print("!result!")
print(result)
}
}