我是服务器和 twilio 的新手,我已将服务器设置为在我的本地主机上正确生成访问 token 。当我使用生成 token 时
http://localhost/index.php?identity=bob&room=example
生成的 token 位于 jwt 中,我得到了正确的 token ,因为如果我直接将其放入应用程序中,我会连接,但是当我尝试使用该函数调用它时:
(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
这是生成的 token :
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
)
此外,当我直接提供此 token 时,连接可以正常工作,但通过 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();
最佳答案
此处为 Twilio 开发人员布道师。
在服务器端代码中,您将返回一个简单的字符串,而不是 JSON。
您现在有两个选择。您可以将返回的 data
转换为 NSString
,这将是您的 token 。不过,这不会包括您当前正在记录的身份。要也获得身份,您可以更改服务器的返回值以实际返回带有 token 和身份的 JSON。那么您的 JSON 解析代码应该可以工作。
对于选项 1,您需要将 NSData
转换为 NSString
。你应该可以这样做:
NSString *accessToken = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
或者,如果您保留 JSON 解析代码并从服务器返回 JSON,请将 PHP 的最后几行更改为:
$grant = new VideoGrant();
$grant->setRoom($room);
$token->addGrant($grant);
$data = array('identity'=>$identity, 'token'=>$token->toJWT());
header('Content-type: application/json');
echo json_encode($data);
如果这有帮助,请告诉我。
关于ios - JSON 字符串没有得到正确的值返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44663238/