我需要使用 Twitter API 1.1 更新我的应用程序。以前我使用以下 URL 检索推文:
但现在它不起作用。我尝试使用 Twitter API 文档中的以下 URL:
但它返回以下错误:
{"errors":[{"message":"Bad Authentication data","code":215}]}
如何检索这些推文?
我想使用 Twitter.framework,但任何建议都将不胜感激。
我需要使用 Twitter API 1.1 更新我的应用程序。以前我使用以下 URL 检索推文:
但现在它不起作用。我尝试使用 Twitter API 文档中的以下 URL:
但它返回以下错误:
{"errors":[{"message":"Bad Authentication data","code":215}]}
如何检索这些推文?
我想使用 Twitter.framework,但任何建议都将不胜感激。
我找到了解决方案。Twitter 接受两种类型的身份验证来检索推文:
bearer token
.起初我的请求是这样的:
NSURL *URL = [NSURL URLWithString:@"http://search.twitter.com/search.json?tag=ios&rpp=25"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
现在看起来如下:
if(self.bearerToken == nil) return;
NSURL *URL = [NSURL URLWithString:@"https://api.twitter.com/1.1/search/tweets.json?q=%%23ios&count=25"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
[urlRequest setValue:[NSString stringWithFormat:@"Bearer %@", self.bearerToken] forHTTPHeaderField:@"Authorization"];
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
此方法还需要添加两个方法bearerToken
和_base64Encode
:
- (NSString *)bearerToken
{
if(_bearerToken == nil)
{
NSString * consumerKey = [config.consumerKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString * consumerSecret = [config.consumerSecret stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//the combined authentication key is "CONSUMER_KEY:CONSUMER_SECRET" run through base64 encoding.
//we'll use NSData instead of NSString here so that we can feed it directly to the HTTPRequest later.
NSString * combinedKey = [[self class] _base64Encode:[[NSString stringWithFormat:@"%@:%@", consumerKey, consumerSecret] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.twitter.com/oauth2/token"]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:[NSString stringWithFormat:@"Basic %@", combinedKey] forHTTPHeaderField:@"Authorization"];
[urlRequest setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded;charset=UTF-8"] forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:[@"grant_type=client_credentials" dataUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
_bearerToken = [responseJSON valueForKey:@"access_token"];
}
return _bearerToken;
}
+(NSString *)_base64Encode:(NSData *)data{
//Point to start of the data and set buffer sizes
int inLength = [data length];
int outLength = ((((inLength * 4)/3)/4)*4) + (((inLength * 4)/3)%4 ? 4 : 0);
const char *inputBuffer = [data bytes];
char *outputBuffer = malloc(outLength);
outputBuffer[outLength] = 0;
//64 digit code
static char Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//start the count
int cycle = 0;
int inpos = 0;
int outpos = 0;
char temp;
//Pad the last to bytes, the outbuffer must always be a multiple of 4
outputBuffer[outLength-1] = '=';
outputBuffer[outLength-2] = '=';
/* http://en.wikipedia.org/wiki/Base64
Text content M a n
ASCII 77 97 110
8 Bit pattern 01001101 01100001 01101110
6 Bit pattern 010011 010110 000101 101110
Index 19 22 5 46
Base64-encoded T W F u
*/
while (inpos < inLength){
switch (cycle) {
case 0:
outputBuffer[outpos++] = Encode[(inputBuffer[inpos]&0xFC)>>2];
cycle = 1;
break;
case 1:
temp = (inputBuffer[inpos++]&0x03)<<4;
outputBuffer[outpos] = Encode[temp];
cycle = 2;
break;
case 2:
outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xF0)>> 4];
temp = (inputBuffer[inpos++]&0x0F)<<2;
outputBuffer[outpos] = Encode[temp];
cycle = 3;
break;
case 3:
outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xC0)>>6];
cycle = 4;
break;
case 4:
outputBuffer[outpos++] = Encode[inputBuffer[inpos++]&0x3f];
cycle = 0;
break;
default:
cycle = 0;
break;
}
}
NSString *pictemp = [NSString stringWithUTF8String:outputBuffer];
free(outputBuffer);
return pictemp;
}
响应也已更改,因此我也必须更改解析器。以前的版本:
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithString:jsonStr];
if (responseDictionary)
{
id data = responseDictionary[@"results"];
if ([data isKindOfClass:NSArray.class])
{
NSArray *dataArray = (NSArray*)data;
for (NSDictionary *post in dataArray)
{
avatarUrl = post[@"profile_image_url"];
author = post[@"from_user"];
message = post[@"text"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"EEE',' dd MMM yyyy HH:mm:ss ZZZZ"];
date = [inputFormatter dateFromString: post[@"created_at"]];
}
}
}
当前版本:
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithString:jsonStr];
if (responseDictionary)
{
id data = [responseDictionary valueForKey:@"statuses"];
if ([data isKindOfClass:NSArray.class])
{
NSArray *dataArray = (NSArray*)data;
for (NSDictionary *post in dataArray)
{
avatarUrl = post[@"user"][@"profile_image_url"];
author = post[@"user" ][@"name"];;
message = post[@"text"];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZZ yyyy"];
date = [inputFormatter dateFromString: post[@"created_at"]];
}
}
}
根据这个文档,Hashtag feed
没有Authentication
.
但是您可以在服务器端对其进行管理。在服务器端,您可以Authenticate
打开Twitter
并Hash tag feed
从 Twitter 获取(此处还需要验证才能获取提要)。因此,每当用户不在Authenticate
那个iPhone
时间时,您必须致电Web Service
,Web 服务将返回HashTag feed
您在服务器端代码中管理的内容。
如果已经在 iOS 设备 (iPhone) 上使用了 Authenticate,那么您可以使用 iOS 获取提要。