【问题标题】:401 Unauthorized using oauth with twitter on iPhone401 未经授权在 iPhone 上使用 oauth 和 Twitter
【发布时间】:2011-02-21 21:38:27
【问题描述】:

我使用Twitter-OAuth-iPhone

当我尝试使用 SA_OAuthTwitterEngine 登录 Twitter 时,我收到了错误 401 我收到了代表要求:

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {

但是当我尝试在之后发送推文时:

[_engine sendUpdate: [NSString stringWithFormat:@"Hello World"]];

我收到错误 401:

failed with error: Error Domain=HTTP Code=401 "Operation could not be completed. (HTTP error 401.)"

我已经检查了我的 twitter 应用程序设置:

Type:                    Client
Default Access type:     Read & Write

【问题讨论】:

    标签: iphone cocoa twitter oauth http-status-code-401


    【解决方案1】:

    这可能有助于回答您的问题:我刚刚想通了:

    我想我用过 Matt Gemmell 和 his source code 虽然我可能用过 Ben Gottlieb,但我不太记得了。不过很确定是马特。

    不过,我还创建了一个隐藏的子视图,其中包含实际的推文文本字段、推文按钮和返回按钮,以便用户可以再次隐藏该视图。

    总而言之,我有一个按钮,它显示最初隐藏的子视图,并且在执行此操作时,它会验证用户身份。然后,此子视图具有用于发布推文的推文按钮。这是我发现对我有用的代码。现在我只做了最少的测试,但我希望你们可以自己动手并从这里开始。

    -(IBAction)shareTwit:(id)sender { //This shows the tweet view and authorises the user and engine
        tweetView.hidden = NO;
    
        if (!_engine) {
            _engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
            _engine.consumerKey    = kOAuthConsumerKey;
            _engine.consumerSecret = kOAuthConsumerSecret;
        }
    
        if (![_engine isAuthorized]) {
            UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
    
            if (controller) {
                [self presentModalViewController: controller animated: YES];
            }
            else {
                [_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
            }
        }
    }
    
    -(IBAction)updateTwitter:(id)sender { //This button sends the tweet to Twitter.
        if ([_engine isAuthorized]) {
            [_engine sendUpdate:tweetTextField.text];
    
            [tweetTextField resignFirstResponder];
    
            confirmation.text = @"Tweet sent successfully."; //This is an optional UILabel, if you would like the user to know if the tweet has been sent
                                                        // Currently, I haven't created a method where the user is told the sending failed.
        }
        else {
            UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
    
            if (controller) {
                [self presentModalViewController: controller animated: YES];
            }
            else {  
                [_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];   
            }   
        }       
    }
    

    请注意我没有使用viewDidLoad,主要是因为我不同意它。我希望用户首先点击shareTwit 按钮来激活身份验证。然后在启动的视图中,他们可以编写他们的推文。希望对你有帮助!

    【讨论】:

      【解决方案2】:

      在您的 twitter 应用程序配置文件中设置 Callback URL 字段。

      应与:
      $request_token = $connection->getRequestToken(OAUTH_CALLBACK);

      【讨论】:

        【解决方案3】:

        HTTP 401 是“未经授权的”错误。仔细检查您的 Twitter API 设置并确保您使用正确的 kOAuthConsumerSecretkOAuthConsumerKey 常量值。还要确保你的 Twitter 应用注册为“客户端”而不是“浏览器”。

        【讨论】:

          【解决方案4】:

          您应该请求 xAuth 访问 api@twitter.com。

          【讨论】:

          • 怎么样?在 twitter api 控制台中?
          猜你喜欢
          • 2012-04-08
          • 1970-01-01
          • 2017-10-05
          • 1970-01-01
          • 2013-06-15
          • 1970-01-01
          • 2012-06-24
          • 2017-09-27
          • 2012-12-20
          相关资源
          最近更新 更多