GetStream添加活动因403而失败

时间:2019-04-04 09:42:46

标签: swift getstream-io

试用GetStream for Swift,我无法添加活动。

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,并且:

  1. client.currentUserId返回eric(因此,令牌正确吗?)
  2. 未调用ericFeed.add(activity)的回调
  3. 在我的应用程序的仪表板日志上,我看到添加活动的尝试失败,错误403

我尝试了actor: "eric"actor: "user:eric"的不同ID(具有不同的令牌)。可能出了什么问题?

生成令牌(PHP服务器)的代码为:

$userId = "eric";
$client = new GetStream\Stream\Client(<MyApiKey>, <MyAppSecret>);
$userToken = $client->createUserSessionToken($userId);

我收到了要在仪表板上登录的信息:

log1


log2

1 个答案:

答案 0 :(得分:2)

有几件事需要牢记。 首先,可能是在请求结束时释放了您的客户端,这就是为什么不调用回调的原因,但是日志可以显示请求已完成。我建议您使用共享的Client实例,这将很容易使用。要设置共享客户端,您需要编写以下代码:

Client.config = .init(apiKey: "<MyApiKey>", appId: "<MyApiKey>", token: "<Token>")

有关wiki page中客户端设置的更多信息。

第二件事,必须创建/更新Stream用户。从服务器端,您将通过流userId获取令牌,并且可以请求流用户。最简单的方法是在将创建/更新用户的地方调用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参数创建供稿,并且流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)
    }
}