使用iPhone上的oauth与Twitter连接时出现401未授权错误

3

我使用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
4个回答

2
你应该向api@twitter.com请求xAuth访问权限。

怎么做?在 Twitter API 控制台中吗? - Tony

1

这可能有助于回答你的问题:我刚弄清楚:

我想我使用了Matt Gemmell和他的源代码,尽管我可能用了Ben Gottlieb,我记不太清了。但我非常确定是Matt。

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

因此,总结一下,我有一个按钮,它显示最初隐藏的子视图,并在此过程中对用户进行身份验证。然后,这个子视图有用于发布推文的推文按钮。以下是我发现对我有效的代码。现在我只做了最少量的测试,但我希望这足以让你们自己去学习和开发。

-(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按钮来激活身份验证。然后在启动的视图中,他们可以编写自己的推文。希望这能帮到你!

1

HTTP 401 是一种“未授权”错误。请仔细检查您的 Twitter API 设置,并确保您正在使用正确的 kOAuthConsumerSecretkOAuthConsumerKey 常量值。同时,请确保您的 Twitter 应用程序已注册为“客户端”,而不是“浏览器”。


0

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

它应该与以下内容完全相同:
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接