ios - Twitter OAuth 1 POST requests not working -
i creating application ios , using oauth library. requests seem work fine try make post requests following error :
[code 32] not authenticate you.
now not quite sure happening, post requests generated pretty requests. ideas causing error ?
here code generating request:
+ (nsurlrequest *)preparedrequestforpath:(nsstring *)path parameters:(nsdictionary *)queryparameters httpmethod:(nsstring *)httpmethod oauthtoken:(nsstring *)oauth_token oauthsecret:(nsstring *)oauth_token_secret { if (!httpmethod || !oauth_token) return nil; nsmutabledictionary *allparameters = [self standardoauthparameters]; allparameters[@"oauth_token"] = oauth_token; if (queryparameters) [allparameters addentriesfromdictionary:queryparameters]; nsstring *parametersstring = chquerystringfromparameterswithencoding(allparameters, nsutf8stringencoding); nsstring *request_url = api_url; if (path) request_url = [request_url stringbyappendingstring:path]; nsstring *oauth_consumer_secret = consumer_secret; nsstring *basestring = [httpmethod stringbyappendingformat:@"&%@&%@", request_url.utf8andurlencode, parametersstring.utf8andurlencode]; nsstring *secretstring = [oauth_consumer_secret.utf8andurlencode stringbyappendingformat:@"&%@", oauth_token_secret.utf8andurlencode]; nsstring *oauth_signature = [self.class signcleartext:basestring withsecret:secretstring]; allparameters[@"oauth_signature"] = oauth_signature; nsstring *querystring; if (queryparameters) querystring = chquerystringfromparameterswithencoding(queryparameters, nsutf8stringencoding); if (querystring) request_url = [request_url stringbyappendingformat:@"?%@", querystring]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:request_url]]; request.httpmethod = httpmethod; nsmutablearray *parameterpairs = [nsmutablearray array]; [allparameters removeobjectsforkeys:queryparameters.allkeys]; (nsstring *name in allparameters) { nsstring *apair = [name stringbyappendingformat:@"=\"%@\"", [allparameters[name] utf8andurlencode]]; [parameterpairs addobject:apair]; } nsstring *oauthheader = [@"oauth " stringbyappendingformat:@"%@", [parameterpairs componentsjoinedbystring:@", "]]; [request setvalue:oauthheader forhttpheaderfield:@"authorization"]; if ([httpmethod isequaltostring:@"post"] && queryparameters != nil) { nsdata *body = [querystring datausingencoding:nsutf8stringencoding]; [request sethttpbody:body]; } return request; }
edit
the last lines of code set http body seem cause problem. when remove lines
nsdata *body = [querystring datausingencoding:nsutf8stringencoding]; [request sethttpbody:body];
everything works fine, except when parameters exceptionally bigger sending base64 encoded image, fails... possibly fix ?
well...
i don't know cause exactly. can't send private message non-follower.
how api console? can test twitter apis. maybe you.
first test direct message api right way. , compare results between api console , codes.
Comments
Post a Comment