如何使用Twitter API在Twitter上分享?

时间:2014-12-19 06:51:20

标签: ios twitter twitter-oauth

使用誓言API在Twitter上分享我的代码如下:

NSURL* accessTokenUrl = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"];
OAMutableURLRequest* accessTokenRequest = [[OAMutableURLRequest alloc] initWithURL:accessTokenUrl consumer:consumer token:requestToken realm:nil signatureProvider:nil];
OARequestParameter* verifierParam = [[OARequestParameter alloc] initWithName:@"oauth_verifier" value:verifier];

[accessTokenRequest setHTTPMethod:@"POST"];


[accessTokenRequest setValue:@"Test"    forHTTPHeaderField:@"X-Twitter-Client"];
[accessTokenRequest setValue:@"1.0" forHTTPHeaderField:@"X-Twitter-Client-Version"];
[accessTokenRequest setValue:callback     forHTTPHeaderField:@"X-Twitter-Client-URL"];

[accessTokenRequest setParameters:[NSArray arrayWithObject:verifierParam]];
OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
[dataFetcher fetchDataWithRequest:accessTokenRequest
                         delegate:self
                didFinishSelector:@selector(didReceiveRequestToken:data2:)
                  didFailSelector:@selector(didFailOAuth:error:)];

但我在下面得到回复:  {"错误":[{"消息":"无效或过期的令牌","代码":89}]}

但我的令牌有效且未过期,因为我从此成功获得用户的个人资料。 那么有人可以帮助我在这段代码中缺少什么吗?

1 个答案:

答案 0 :(得分:0)

//请尝试使用此代码..

-(IBAction)ShareOnTwitter :(id)sender
{

UIButton *btn4=(UIButton *)sender;
int count = (int)btn4.tag;
NSLog(@"ShareOnTwitter count=%d",count);

UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.jpg",Pathforimage,[[Array_result objectAtIndex:count] objectForKey:@"img_File_Name"]]];

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {

    SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

    SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
    {
        if (result == SLComposeViewControllerResultCancelled)
        {
                    NSLog(@"Cancelled");
        }
        else
        {
            NSLog(@"Done");
        }
        [controller dismissViewControllerAnimated:YES completion:Nil];
    };
    controller.completionHandler =myBlock;

    //Adding the Text to the twitter post value from iOS
    [controller setInitialText:@""];

    //Adding the Image to the twitter post value from iOS
    [controller addImage:image];

    [self presentViewController:controller animated:YES completion:Nil];
}
else
{
    NSLog(@"UnAvailable");
}
}