【问题标题】:GTMOAuth2Authentication Not WorkingGTMOAuth2Authentication 不工作
【发布时间】:2015-05-11 09:25:05
【问题描述】:

我正在尝试使用 Google 的 OAuth2 和 YouTube API。 OAuth 返回 GTMOAuth2Authentication 对象,然后您可以使用该对象向 YouTube 等服务发出请求。我的登录工作正常,当我手动通过身份验证对象时,我可以发出请求。

但是,我也应该能够通过钥匙串访问身份验证对象,并且我收到了一个有效的对象,但是如果我尝试使用它来发出请求,我就不能。我不断收到以下错误:“操作无法完成。(com.google.GTMHTTPFetcher 错误 -1。)”如果有人能指出我的错误,我将不胜感激。我正在真正的 iPhone 5s 上进行测试。

验证码:

GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc]
                                                 initWithScope:scope
                                                 clientID:kGoogleClientID
                                                 clientSecret:kGoogleClientSecret
                                                 keychainItemName:kGoogleKeychainItemName
                                                 delegate:self
                                                 finishedSelector:@selector(viewController:
                                                                            finishedWithAuth:
                                                                            error:)];
[self.navigationController pushViewController:viewController animated:YES];

身份验证完成处理程序:

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error {

    NSLog(@"%s", __PRETTY_FUNCTION__);

    if (error != nil) {
        NSLog(@"%s %@", __PRETTY_FUNCTION__, error.localizedDescription);
        return;
    }

    MediaGETWrapper *getWrapper = [MediaGETWrapper sharedWrapper];
    getWrapper.googleAuth = auth; // passing auth directly without keychain

    [getWrapper youTubeSubscriptionsWithSuccess:nil failure:nil];

YouTube 客户端初始化:

self.youTube = [GTLServiceYouTube new];
GTMOAuth2Authentication *auth = [GTMOAuth2Authentication new];

[GTMOAuth2ViewControllerTouch
 authorizeFromKeychainForName:kGoogleKeychainItemName
 authentication:auth
 error:nil];

self.youTube.authorizer = auth;

请求:

- (void)youTubeSubscriptionsWithSuccess:(void(^)(NSArray *subscriptions))success
                                failure:(void(^)(NSError *error))error {

    NSLog(@"%s", __PRETTY_FUNCTION__);

//    self.youTube.authorizer = self.googleAuth; // If uncommented, works!

    GTLQueryYouTube *query = [GTLQueryYouTube queryForSubscriptionsListWithPart:@"snippet"];
    query.mine = YES;

    [self.youTube
     executeQuery:query
     completionHandler:^(GTLServiceTicket *ticket,
                         GTLYouTubeChannelListResponse *channelList,
                         NSError *error) {

        if (error != nil) {
            NSLog(@"%s %@", __PRETTY_FUNCTION__, error.localizedDescription); // fails here
            return;
        }

        for (GTLYouTubeSubscription *channel in channelList) {
            NSLog(@"%@", channel.snippet.title);
        }
     }];
}

【问题讨论】:

    标签: youtube google-api google-oauth youtube-data-api gtm-oauth2


    【解决方案1】:

    我找不到直接的解决方法,但您可以这样做:

    通过以下方式从GTMOAuth2Authentication 对象获取访问令牌:

    auth.accessToken
    

    然后在您想要发出请求的任何位置设置访问令牌。 要刷新令牌,请使用以下方法:

    [auth 
     authorizeRequest:nil // just to refresh
     completionHandler:^(NSError *error) {
         // your code here
     }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2018-12-22
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多