无法在iOS OAuth1.0,OAConsumer客户端上发布tumblr

无法在iOS OAuth1.0,OAConsumer客户端上发布tumblr,第1张

概述我正在尝试将“tumblr”集成到我的应用程序中.我能够成功获取访问令牌.但是,当我尝试发布时,我收到以下错误 {"meta":{"status":401,"msg":"Not Authorized"},"response":[]} 我正在使用iOS的OAuthConsumer客户端,如果是MGTwitterEngine我已经提取过了. 这就是我尝试过的. #import "ViewControl 我正在尝试将“tumblr”集成到我的应用程序中.我能够成功获取访问令牌.但是,当我尝试发布时,我收到以下错误
{"Meta":{"status":401,"msg":"Not Authorized"},"response":[]}

我正在使用iOS的OAuthConsumer客户端,如果是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

我在这里犯的错误是什么?为什么我收到这个错误?我是否正确地遵循了这些步骤?

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

>您是否能够使用oauth’GET’请求,例如“http://api.tumblr.com/v2/user/info”?
如果您可以收到成功的“GET”请求,那么您的访问令牌有效,您可以查看您发送帖子参数的方式.
>确保您将参数作为http正文以及签名参数传递.库可能提供正确的参数排序.

Nsstring * postbody = @“body = myBodyText& type = text”;

[oRequest sethttpBody:[postbody dataUsingEnCoding:NSUTF8StringEnCoding allowLossyConversion:TRUE]];

总结

以上是内存溢出为你收集整理的无法在iOS OAuth1.0,OAConsumer客户端上发布tumblr全部内容,希望文章能够帮你解决无法在iOS OAuth1.0,OAConsumer客户端上发布tumblr所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1113659.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存