【发布时间】:2017-06-20 21:24:05
【问题描述】:
我是服务器和 twilio 的新手,我已设置服务器以在我的本地主机上正确生成访问令牌。当我使用
生成令牌时http://localhost/index.php?identity=bob&room=example
生成的令牌在 jwt 中,我得到了正确的令牌,因为如果我直接把它放在应用程序中,我会连接,但是当我尝试使用函数调用它时:
(void)retrieveAccessTokenFromURL:(NSString *)tokenURLStr
completion:(void (^)(NSString* token, NSError *err)) completionHandler {
NSURL *tokenURL = [NSURL URLWithString:tokenURLStr];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
NSURLSessionDataTask *task = [session dataTaskWithURL:tokenURL
completionHandler: ^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
NSError *err = error;
NSString *accessToken;
NSString *identity;
if (!err) {
if (data != nil) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&err];
if (!err) {
accessToken = json[@"token"];
identity = json[@"identity"];
NSLog(@"Logged in as %@",identity);
}
}
}
completionHandler(accessToken, err);
}];
[task resume];
}
它会生成一个错误err -> NSError * domain: @"NSCocoaErrorDomain" - code: 3840 0x1559ef60,我检查了它对NSJSONReadingAllowFragments 说,但它仍然给我同样的错误,而直接放入相同的令牌时效果很好。如果有人可以帮助我,那就太好了。提前致谢。
编辑 1
这是生成的令牌:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS2I2M2RjMzE2Yjc0ZDdhN2E0YzEyNjYzNDJlNTY2MTcwLTE0OTc5OTIzMTMiLCJpc3MiOiJTS2I2M2RjMzE2Yjc0ZDdhN2E0YzEyNjYzNDJlNTY2MTcwIiwic3ViIjoiQUM5ZTliNDYxZDI4NDkzZWY2ODYwNDMzYzViZWRkOTk0YyIsImV4cCI6MTQ5Nzk5NTkxMywiZ3JhbnRzIjp7ImlkZW50aXR5IjoiYm9iIiwidmlkZW8iOnsicm9vbSI6ImV4YW1wbGUifX19.Ppe85LeD8CFatGXkXgzaTR_ljznXIrpyrb8lu3SR4xo
我在 jwt.io 上查过
HEADER:ALGORITHM & TOKEN TYPE
{
"typ": "JWT",
"alg": "HS256",
"cty": "twilio-fpa;v=1"
}
PAYLOAD:DATA
{
"jti": "SKb63dc316b74d7a7a4c1266342e566170-1497992313",
"iss": "SKb63dc316b74d7a7a4c1266342e566170",
"sub": "AC9e9b461d28493ef6860433c5bedd994c",
"exp": 1497995913,
"grants": {
"identity": "bob",
"video": {
"room": "example"
}
}
}
VERIFY SIGNATURE
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret
)
另外,当我直接给这个令牌时,连接工作,但通过 url 它不工作。
网站代码:
<?php
include('./vendor/autoload.php');
include('./config.php');
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
// Use identity and room from query string if provided
$identity = isset($_GET["identity"]) ? $_GET["identity"] : "identity";
$room = isset($_GET["room"]) ? $_GET["room"] : "";
// Create access token, which we will serialize and send to the client
$token = new AccessToken(
$TWILIO_ACCOUNT_SID,
$TWILIO_API_KEY,
$TWILIO_API_SECRET,
3600,
$identity
);
// Grant access to Video
$grant = new VideoGrant();
$grant->setRoom($room);
$token->addGrant($grant);
echo $token->toJWT();
【问题讨论】:
-
你能分享一下 PHP 服务器生成的 JSON 对象是什么样子的吗?
-
编辑更新有问题。谢谢
-
您是以字符串还是 JSON 对象的形式返回数据?
-
我使用的代码与 twilio 网站上用于 php 的快速入门教程中的代码相同。我也会为此更新代码
-
用 php 文件中的代码更新了代码
标签: ios objective-c json twilio twilio-php