首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS Youtube实时流API错误

iOS Youtube实时流API错误
EN

Stack Overflow用户
提问于 2017-06-15 12:19:21
回答 1查看 665关注 0票数 1

我正在开发一个应用程序,允许用户在Youtube上直播。我已经用作用域实现了OAuth 2.0登录-

googleapis.com/auth/youtube.force-ssl /auth/youtube

现在显示的是这样的:

在成功登录到我的应用程序后,我想用API- https://www.googleapis.com/youtube/v3/liveStreams创建实时流并发布数据。请检查我下面的代码片段-

代码语言:javascript
复制
    GTMSessionFetcherService *fetcherService = [[GTMSessionFetcherService alloc] init];
    fetcherService.authorizer = self.authorization;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@{@"title":@"FS New Vod", @"description":@"hahahahah its coming now"} forKey:@"snippet"];
    [dict setValue:@{@"format":@"360p", @"ingestionType":@"rtmp"} forKey:@"cdn"];
    [dict setValue:@{@"isReusable":@"false"} forKey:@"contentDetails"];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    NSMutableURLRequest * req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[@"https://www.googleapis.com/youtube/v3/liveStreams?part=cdn,snippet,contentDetails&onBehalfOfContentOwner=sdfdsfds&onBehalfOfContentOwnerChannel=Ankush_FS&key=" stringByAppendingString:self.authorization.authState.lastTokenResponse.accessToken]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    req.HTTPMethod = @"POST";
    GTMSessionFetcher *ft = [fetcherService fetcherWithRequest:req];
    ft.bodyData = jsonData;
    [ft beginFetchWithCompletionHandler:^(NSData * _Nullable data, NSError * _Nullable error) {
        if (error) {
            // OIDOAuthTokenErrorDomain indicates an issue with the authorization.
            if ([error.domain isEqual:OIDOAuthTokenErrorDomain]) {
                [self setGtmAuthorization:nil];
                [self logMessage:@"Authorization error during token refresh, clearing state. %@", error];
                // Other errors are assumed transient.
            } else {
                [self logMessage:@"Transient error during token refresh. %@", error];
            }
            return;
        }
        NSError *jsonError = nil;
        id jsonDictionaryOrArray =
        [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];

        // JSON error.
        if (jsonError) {
            [self logMessage:@"JSON decoding error %@", jsonError];
            return;
        }

        // Success response!
        [self logMessage:@"Success: %@", jsonDictionaryOrArray];
    }];

但每次出错-

代码语言:javascript
复制
Example-iOS[5413:160436] Transient error during token refresh. Error Domain=com.google.HTTPStatus Code=400 "(null)" UserInfo={data=<7b0a2022 6572726f 72223a20 7b0a2020 22657272 6f727322 3a205b0a 2020207b 0a202020 2022646f 6d61696e 223a2022 676c6f62 616c222c 0a202020 20227265 61736f6e 223a2022 70617273 65457272 6f72222c 0a202020 20226d65 73736167 65223a20 22546869 73204150 4920646f 6573206e 6f742073 7570706f 72742070 61727369 6e672066 6f726d2d 656e636f 64656420 696e7075 742e220a 2020207d 0a20205d 2c0a2020 22636f64 65223a20 3430302c 0a202022 6d657373 61676522 3a202254 68697320 41504920 646f6573 206e6f74 20737570 706f7274 20706172 73696e67 20666f72 6d2d656e 636f6465 6420696e 7075742e 220a207d 0a7d0a>}

我在这里做什么坏事?还有其他方式来访问youtube api服务吗?

帮帮忙!

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-06-21 05:40:00

经过大量的研究,我找到了解决办法。我们需要调用Youtube数据API,并且应该在URL字符串中提供API密钥和访问令牌。我也在张贴代码片段。在google登录时,需要为google控制台中的特定应用程序添加范围和数据api。

代码语言:javascript
复制
@IBAction func btnYoutubeLiveDidTapped(_ sender: Any) {
    loaderWillShow(status: true)
    var scopes = GIDSignIn.sharedInstance().scopes
    scopes?.append("https://www.googleapis.com/auth/youtube")
    scopes?.append("https://www.googleapis.com/auth/youtube.force-ssl")
    scopes?.append("https://www.googleapis.com/auth/youtube.upload")
    scopes?.append("https://www.googleapis.com/auth/youtubepartner")
    GIDSignIn.sharedInstance().scopes = scopes
    GIDSignIn.sharedInstance().signIn()
}


func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
    loaderWillShow(status: false)
    if (error == nil) {
        let alert = UIAlertController(title: "Broadcast Details", message: "Kindly provide us the following details.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addTextField(configurationHandler: { (tf: UITextField) in
            tf.placeholder = "Title (required)"
        })
        alert.addTextField(configurationHandler: { (tf: UITextField) in
            tf.placeholder = "Description"
        })
        let actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
        let actionOkay = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
            if !(alert.textFields?.first?.text?.isEmpty)! {
                DispatchQueue.main.async {
                    self.loaderWillShow(status: true)
                }
                var dictData = [String: Any]()
                dictData["snippet"] = ["title": alert.textFields?.first?.text!, "description": alert.textFields?[1].text == "" ? DEFAULT_VIDEO_DESC : alert.textFields?[1].text!]
                dictData["cdn"] = ["format": "480p", "ingestionType": "rtmp"]
                dictData["contentDetails"] = ["isReusable": "false"]
                let apiMngr = APIManager()
                apiMngr.requestType = .Post
                var url = URL(string: "https://www.googleapis.com/youtube/v3/liveStreams?part=cdn,snippet,contentDetails&key=\(GOOGLE_API_KEY)&access_token=\(signIn.currentUser.authentication.accessToken!)")!
                apiMngr.startDataRequestWith(url: url, data: dictData, completion: { (response: Data?, error: Error?) in
                    if error == nil, let dicStream = apiMngr.getResponseDictionary() {
                        print(dicStream)
                        let formatter = DateFormatter()
                        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.0Z"
                        dictData = [String: Any]()
                        dictData["snippet"] = ["title": (dicStream["snippet"] as! Dictionary<String, Any>)["title"] as! String, "description": (dicStream["snippet"] as! Dictionary<String, Any>)["description"] as! String, "scheduledStartTime": formatter.string(from: Date())]
                        dictData["status"] = ["privacyStatus": "public"]
                        url = URL(string: "https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet,status&key=\(GOOGLE_API_KEY)&access_token=\(signIn.currentUser.authentication.accessToken!)")!
                        let apiMngr2 = APIManager()
                        apiMngr2.requestType = .Post
                        apiMngr2.startDataRequestWith(url: url, data: dictData, completion: { (response: Data?, error: Error?) in
                            if error == nil, let dicBroadcast = apiMngr2.getResponseDictionary() {
                                print(dicBroadcast)
                                url = URL(string: "https://www.googleapis.com/youtube/v3/liveBroadcasts/bind?part=id,snippet,contentDetails,status&id=\(dicBroadcast["id"] as! String)&streamId=\(dicStream["id"] as! String)&key=\(GOOGLE_API_KEY)&access_token=\(signIn.currentUser.authentication.accessToken!)")!
                                let apiMngr3 = APIManager()
                                apiMngr3.requestType = .Post
                                apiMngr3.startDataRequestWith(url: url, data: [:], completion: { (response: Data?, error: Error?) in
                                    if error == nil, let dicBroadcastBind = apiMngr3.getResponseDictionary() {
                                        print(dicBroadcastBind)
                                        self.imgLiveFrom.image = UIImage(named: "YouTube.png")
                                        self.streamURL = URL(string: "\((((dicStream["cdn"] as! Dictionary<String, Any>)["ingestionInfo"] as! Dictionary<String, String>)["ingestionAddress"])!)/\((((dicStream["cdn"] as! Dictionary<String, Any>)["ingestionInfo"] as! Dictionary<String, String>)["streamName"])!)")
                                        self.sendStreamURLToServer()
                                    } else {
                                        DispatchQueue.main.async {
                                            self.loaderWillShow(status: false)
                                            self.showAlert(msg: ALERT_MESSAGE_PUBLISH_ERROR)
                                        }
                                    }
                                })
                            } else {
                                DispatchQueue.main.async {
                                    self.loaderWillShow(status: false)
                                    self.showAlert(msg: ALERT_MESSAGE_PUBLISH_ERROR)
                                }
                            }
                        })
                    } else {
                        DispatchQueue.main.async {
                            self.loaderWillShow(status: false)
                            self.showAlert(msg: ALERT_MESSAGE_PUBLISH_ERROR)
                        }
                    }
                })
            } else {
                let alert2 = UIAlertController(title: ALERT_TITLE, message: ALERT_MESSAGE_YOUTUBE_TITLE, preferredStyle: UIAlertControllerStyle.alert)
                let actionCancel = UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)
                DispatchQueue.main.async {
                    self.present(alert2, animated: true, completion: nil)
                }
                alert2.addAction(actionCancel)
            }
        })
        alert.addAction(actionCancel)
        alert.addAction(actionOkay)
        present(alert, animated: true, completion: nil)
    } else {
        self.showAlert(msg: ALERT_MESSAGE_PUBLISH_ERROR)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44567352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档