无法从 iOS OAuth1.0、OAConsumer 客户端在 tumblr 上发帖 [英] Unable to post on tumblr from iOS OAuth1.0, OAConsumer client

查看:40
本文介绍了无法从 iOS OAuth1.0、OAConsumer 客户端在 tumblr 上发帖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将tumblr"集成到我的应用程序中.我能够成功获取访问令牌.但是,当我尝试发布时,出现以下错误

I am trying to integrate "tumblr" into my application.I am able to get the access token successfully. But, when I try to post, I am getting the following error

{"meta":{"status":401,"msg":"Not Authorized"},"response":[]}

我正在使用 iOS 的 OAuthConsumer 客户端,如果是从 MGTwitterEngine 中提取的.

I am using the OAuthConsumer client for iOS, which I have pulled if from MGTwitterEngine.

这是我试过的.

#import "ViewController.h"


#define consumer_key  @"u9iZvT8KIlrTtUrh3vUeXXXXXXXXXXXXXAfgpThGyom8Y6MKKCnU"
#define consumer_secret  @"xfA10mQKmALlpsnrFXXXXXXXXXXXXXXXXXXXXXXXXXX"
#define request_token_url  @"http://www.tumblr.com/oauth/request_token"
#define access_token_url  @"http://www.tumblr.com/oauth/access_token"
#define authorize_url  @"http://www.tumblr.com/oauth/authorize?oauth_token=%@"
#define base_url @"http://api.tumblr.com/v2/user/XXXXXXXXXXXXX.tumblr.com/info"
#define user_info @"http://api.tumblr.com/v2/user/info"

@interface ViewController ()

@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
}




- (IBAction)postIt:(id)sender
{

    NSURL *postURL = [NSURL URLWithString:@"http://api.tumblr.com/v2/blog/xxxxxxxx.tumblr.com/post"];
    OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:postURL
                                                                    consumer:self.consumer
                                                                       token:self.accessToken
                                                                       realm:nil
                                                           signatureProvider:nil];
    [oRequest setHTTPMethod:@"POST"];

    [oRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    OARequestParameter *statusParam = [[OARequestParameter alloc] initWithName:@"body"
                                                                         value:@"Sample Body"];
    OARequestParameter *statusParam2 = [[OARequestParameter alloc] initWithName:@"type"
                                                                         value:@"text"];

    NSArray *params = [NSArray arrayWithObjects:statusParam,statusParam2, nil];
    [oRequest setParameters:params];
    OAAsynchronousDataFetcher *fetcher = [OAAsynchronousDataFetcher asynchronousFetcherWithRequest:oRequest
                                                                                          delegate:self
                                                                                 didFinishSelector:@selector(sendStatusTicket:didFinishWithData:)
                                                                                   didFailSelector:@selector(sendStatusTicket:didFailWithError:)];
    NSLog(@"URL = %@",[oRequest.URL absoluteString]);

    [fetcher start];
}


- (void)didReceiveAccessToken:(OAServiceTicket *)ticker data:(NSData *)responseData
{


}

- (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {
    // ERROR!
}




- (void)sendStatusTicket:(OAServiceTicket *)ticker didFinishWithData:(NSData *)responseData
{
    if (ticker.didSucceed) {
        NSLog(@"Success");
    }
    NSString *responseBody = [[NSString alloc] initWithData:responseData
                                                   encoding:NSUTF8StringEncoding];

    NSLog(@"Description = %@",responseBody);


}
- (void)sendStatusTicket:(OAServiceTicket *)ticker didFailWithError:(NSError *)error
{
    NSLog(@"Error = %@",[error localizedDescription]);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)login:(id)sender
{
    self.consumer = [[OAConsumer alloc] initWithKey:consumer_key secret:consumer_secret];

    NSURL *url = [NSURL URLWithString:request_token_url];


    OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
                                                                   consumer:self.consumer
                                                                      token:nil   // we don't have a Token yet
                                                                      realm:nil   // our service provider doesn't specify a realm
                                                          signatureProvider:nil]; // use the default method, HMAC-SHA1

    [request setHTTPMethod:@"POST"];

    OADataFetcher *fetcher = [[OADataFetcher alloc] init];

    [fetcher fetchDataWithRequest:request
                         delegate:self
                didFinishSelector:@selector(requestTokenTicket:didFinishWithData:)
                  didFailSelector:@selector(requestTokenTicket:didFailWithError:)];


}
- (void)requestTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
    if (ticket.didSucceed)
    {
        NSString *responseBody = [[NSString alloc] initWithData:data
                                                       encoding:NSUTF8StringEncoding];
        self.accessToken= [[OAToken alloc] initWithHTTPResponseBody:responseBody];

        NSURL *author_url = [NSURL URLWithString:[ NSString stringWithFormat:authorize_url,self.accessToken.key]];
        OAMutableURLRequest  *oaR = [[OAMutableURLRequest alloc] initWithURL:author_url consumer:nil token:nil realm:nil signatureProvider:nil];        
        UIWebView  *webView =[[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [[[UIApplication sharedApplication] keyWindow] addSubview:webView];
        webView.delegate=self;
        [webView loadRequest:oaR];

    }
}

// This is to get oAuth_verifier from the url

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

    NSString *url = [[request URL] absoluteString];
    NSString *keyOne = @"oauth_token";
    NSString *keyTwo = @"oauth_verifier";
    NSRange r1 =[url rangeOfString:keyOne];
    NSRange r2 =[url rangeOfString:keyTwo];
    if (r1.location!=NSNotFound && r2.location!=NSNotFound) {
        // Extract oauth_verifier from URL query
        NSString* verifier = nil;
        NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:@"&"];
        for (NSString* param in urlParams) {
            NSArray* keyValue = [param componentsSeparatedByString:@"="];
            NSString* key = [keyValue objectAtIndex:0];
            if ([key isEqualToString:@"oauth_verifier"]) {
                verifier = [keyValue objectAtIndex:1];
                break;
            }
        }
        if (verifier) {
            NSURL* accessTokenUrl = [NSURL URLWithString:@"http://www.tumblr.com/oauth/access_token"];
            OAMutableURLRequest* accessTokenRequest =[[OAMutableURLRequest alloc] initWithURL:accessTokenUrl
                                                                                     consumer:self.consumer
                                                                                        token:self.accessToken
                                                                                        realm:nil
                                                                            signatureProvider:nil];
            OARequestParameter* verifierParam =[[OARequestParameter alloc] initWithName:@"oauth_verifier" value:verifier];
            [accessTokenRequest setHTTPMethod:@"POST"];
            [accessTokenRequest setParameters:[NSArray arrayWithObjects:verifierParam,nil]];
            OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
            [dataFetcher fetchDataWithRequest:accessTokenRequest
                                     delegate:self
                            didFinishSelector:@selector(requestTokenTicketForAuthorization:didFinishWithData:)
                              didFailSelector:@selector(requestTokenTicket:didFailWithError:)];
        } else {
            // ERROR!
        }
        [webView removeFromSuperview];
        return NO;
    }
    return YES;
}


- (void)requestTokenTicketForAuthorization:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data
{
    if (ticket.didSucceed)
    {
        NSString *responseBody = [[NSString alloc] initWithData:data
                                                       encoding:NSUTF8StringEncoding];
        self.accessToken = [self.accessToken initWithHTTPResponseBody:responseBody];
        accessText=self.accessToken.key;
        accessSecret=self.accessToken.secret;
    }
    else
    {
        NSString *responseBody = [[NSString alloc] initWithData:data
                                                       encoding:NSUTF8StringEncoding];
        NSLog(@"Response = %@",responseBody);
    }


}
- (void)requestTokenTicket:(OAServiceTicket *)ticket didFailWithError:(NSError *)error
{
    NSLog(@"Error = %@",[error localizedDescription]);
}


@end

我在这里犯了什么错误?为什么我收到那个错误?我是否按照正确的步骤操作?

Whats the mistake I am making here? Why I am getting that error? Did I follow the steps properly?

推荐答案

请 XXX 输出您的 consumer_key 和 consumer_secret 以避免不必要的使用它们.在代码方面,您可能需要在这里寻找一些东西.

Please XXX out your consumer_key and consumer_secret to avoid unwanted use of them. Code wise there are a few things you might want to look for here.

  1. 您是否可以使用 oauth 'GET' 请求,例如 "http://api.tumblr.com/v2/user/info"?如果您可以收到成功的GET"请求,那么您的访问令牌是有效的,您可以查看您发送帖子参数的方式.

  1. Are you able to use an oauth 'GET' request such as "http://api.tumblr.com/v2/user/info"? If you can receive a successful 'GET' request then your access token is valid and you can look at how you're sending your post parameters.

确保您将参数作为 HTTP 正文和签名参数传递.库可能会提供正确的参数排序.

Make sure you are passing in your parameters as HTTP Body as well as signature parameters. Correct parameter ordering is likely provided by the library.

NSString *postbody = @"body=myBodyText&type=text";

NSString *postbody = @"body=myBodyText&type=text";

[oRequest setHTTPBody:[postbody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:TRUE]];

[oRequest setHTTPBody:[postbody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:TRUE]];

这篇关于无法从 iOS OAuth1.0、OAConsumer 客户端在 tumblr 上发帖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆