iPhone 和 Twitter API OAuth 消费者密钥和秘密
我从 Twitter 加载数据时遇到问题。我在下面发布的方法大约需要 5-10 分钟,然后它会冻结我的消费者密钥......
我知道这是密钥,因为我已经在设备上安装了该应用程序,然后我将进行更改并在模拟器上运行它也会影响设备。这持续了大约 30 分钟,然后按键似乎会自行刷新并工作,而无需对实际代码进行任何更改。有时我会进入 Twitter Dev(如果我的应用程序是这样的)并重置密钥并替换代码中的密钥,这会工作大约 10 分钟,然后会发生同样的事情!
下面是我用来调用 twitter OAuth 并接收 UITableView
对象的代码。
- (void)viewDidAppear:(BOOL)animated
{
if(_engine) return;
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = @"key";
_engine.consumerSecret = @"key";
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
else
{
userAvatar.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a2.twimg.com/profile_images/1361674958/Wintboard_200px.png" ]]];
tweets = [[NSMutableArray alloc] init];
authors = [[NSMutableArray alloc] init];
avatarsURL =[[NSMutableArray alloc] init];
friendsCount = [[NSMutableArray alloc] init];
followerCount = [[NSMutableArray alloc] init];
retweetCount = [[NSMutableArray alloc] init];
//retweetCount = [[NSMutableArray alloc] init];
[self updateStream:nil];
[progressView.view removeFromSuperview];
}
}
#pragma mark IBActions
-(IBAction)updateStream:(id)sender
{
// [_engine sendUpdate:[textfield text]];
[_engine getFollowedTimelineSinceID:0 startingAtPage:0 count:100];
//[_twitter getFollowedTimelineSinceID:0 startingAtPage:0 count:0];
NSString *path = [[NSBundle mainBundle] pathForResource:@"unlock" ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
-(IBAction)tweet:(id)sender
{
[_engine sendUpdate:[textfield text]];
[self updateStream:nil];
if (textfield isFirstResponder])
{
textfield.text = @"";
[textfield resignFirstResponder];
}
}
-(BOOL)textFieldShouldClear:(UITextField*)textfield
{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// the user pressed the "Done" button, so dismiss the keyboard
[textField resignFirstResponder];
return YES;
}
// Return YES for supported orientations
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
{
return YES;
}
#pragma mark UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// [_engine sendUpdate:[textfield text]];
[_engine getFollowedTimelineSinceID:0 startingAtPage:0 count:100];
int count = [tweets count];
int count2 = [authors count];
int count3 = [avatarsURL count];
//Return just enough cells to fill the screen during loading ....
if (count == 0)
count = MyCustomRowCount;
return count = 100;
return count2 = 100;
return count3 = 100;
// return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}
/*
//Here it adds a nice shadow to the table view but will crash on rotation and send a wird dump
tableView.layer.shadowColor = [[UIColor blackColor] CGColor];
tableView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
tableView.layer.shadowRadius = 8.0f;
tableView.layer.shadowOpacity = 1.0f;
*/
[cell.textLabel setNumberOfLines:1];
[cell.textLabel setText:[(Tweet*)[authors objectAtIndex:indexPath.row] author]];
[cell.detailTextLabel setText:[(Tweet*)[tweets objectAtIndex:indexPath.row] tweet]];
[cell.detailTextLabel setNumberOfLines:10];
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
[cell.textLabel setShadowColor:[UIColor whiteColor]];
[cell.textLabel setShadowOffset:CGSizeMake(0.5, 0.5)];
[cell.detailTextLabel setTextColor:[UIColor blackColor]];
//[cell.detailTextLabel setText:[(Tweet*)[retweetCount objectAtIndex:indexPath.row] reTweetCount]];
[cell.textLabel setUserInteractionEnabled:YES];
[cell.contentView setMultipleTouchEnabled:YES];
// Here we use the new provided setImageWithURL: method to load the web image with SDWebImageManager
[cell.imageView setImageWithURL:[NSURL URLWithString:[(Tweet*)[avatarsURL objectAtIndex:indexPath.row]avatarURL]]
placeholderImage:[UIImage imageNamed:@"avatar.png"]];
//add gradient to cell
UIImage *gradient = [UIImage imageNamed:@"gradientcell2.png"];
UIImageView *cellimage = [[UIImageView alloc] initWithImage:gradient];
cellimage.contentMode = UIViewContentModeScaleToFill;
cell.backgroundView = cellimage;
[cellimage release];
UIImage *selectedGradient = [UIImage imageNamed:@"selectedcell.png"];
UIImageView *selectedCell = [[UIImageView alloc] initWithImage:selectedGradient];
selectedCell.contentMode = UIViewContentModeScaleToFill;
cell.selectedBackgroundView = selectedCell;
[tableView setBackgroundColor:[UIColor clearColor]];
return cell;
}
I have a issue while loading my data from Twitter. The method I posted below works for about 5-10 minutes then it freezes up my consumer keys....
I know it is the keys because I have the app already installed on the device working then I will make changes and run on the simulator and it affects the device as well. This last for about 30 minutes then the keys seem to refresh themselves and work without any changes to the actual code. Sometimes I will just go in Twitter Dev were my application is and reset the keys and replace the keys in my code and that works for about 10 minutes then the same thing happens!
Here is the code I am using to call out to twitter OAuth and receive the objects for my UITableView
.
- (void)viewDidAppear:(BOOL)animated
{
if(_engine) return;
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = @"key";
_engine.consumerSecret = @"key";
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
else
{
userAvatar.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://a2.twimg.com/profile_images/1361674958/Wintboard_200px.png" ]]];
tweets = [[NSMutableArray alloc] init];
authors = [[NSMutableArray alloc] init];
avatarsURL =[[NSMutableArray alloc] init];
friendsCount = [[NSMutableArray alloc] init];
followerCount = [[NSMutableArray alloc] init];
retweetCount = [[NSMutableArray alloc] init];
//retweetCount = [[NSMutableArray alloc] init];
[self updateStream:nil];
[progressView.view removeFromSuperview];
}
}
#pragma mark IBActions
-(IBAction)updateStream:(id)sender
{
// [_engine sendUpdate:[textfield text]];
[_engine getFollowedTimelineSinceID:0 startingAtPage:0 count:100];
//[_twitter getFollowedTimelineSinceID:0 startingAtPage:0 count:0];
NSString *path = [[NSBundle mainBundle] pathForResource:@"unlock" ofType:@"caf"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
-(IBAction)tweet:(id)sender
{
[_engine sendUpdate:[textfield text]];
[self updateStream:nil];
if (textfield isFirstResponder])
{
textfield.text = @"";
[textfield resignFirstResponder];
}
}
-(BOOL)textFieldShouldClear:(UITextField*)textfield
{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// the user pressed the "Done" button, so dismiss the keyboard
[textField resignFirstResponder];
return YES;
}
// Return YES for supported orientations
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
{
return YES;
}
#pragma mark UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// [_engine sendUpdate:[textfield text]];
[_engine getFollowedTimelineSinceID:0 startingAtPage:0 count:100];
int count = [tweets count];
int count2 = [authors count];
int count3 = [avatarsURL count];
//Return just enough cells to fill the screen during loading ....
if (count == 0)
count = MyCustomRowCount;
return count = 100;
return count2 = 100;
return count3 = 100;
// return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}
/*
//Here it adds a nice shadow to the table view but will crash on rotation and send a wird dump
tableView.layer.shadowColor = [[UIColor blackColor] CGColor];
tableView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
tableView.layer.shadowRadius = 8.0f;
tableView.layer.shadowOpacity = 1.0f;
*/
[cell.textLabel setNumberOfLines:1];
[cell.textLabel setText:[(Tweet*)[authors objectAtIndex:indexPath.row] author]];
[cell.detailTextLabel setText:[(Tweet*)[tweets objectAtIndex:indexPath.row] tweet]];
[cell.detailTextLabel setNumberOfLines:10];
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
[cell.textLabel setShadowColor:[UIColor whiteColor]];
[cell.textLabel setShadowOffset:CGSizeMake(0.5, 0.5)];
[cell.detailTextLabel setTextColor:[UIColor blackColor]];
//[cell.detailTextLabel setText:[(Tweet*)[retweetCount objectAtIndex:indexPath.row] reTweetCount]];
[cell.textLabel setUserInteractionEnabled:YES];
[cell.contentView setMultipleTouchEnabled:YES];
// Here we use the new provided setImageWithURL: method to load the web image with SDWebImageManager
[cell.imageView setImageWithURL:[NSURL URLWithString:[(Tweet*)[avatarsURL objectAtIndex:indexPath.row]avatarURL]]
placeholderImage:[UIImage imageNamed:@"avatar.png"]];
//add gradient to cell
UIImage *gradient = [UIImage imageNamed:@"gradientcell2.png"];
UIImageView *cellimage = [[UIImageView alloc] initWithImage:gradient];
cellimage.contentMode = UIViewContentModeScaleToFill;
cell.backgroundView = cellimage;
[cellimage release];
UIImage *selectedGradient = [UIImage imageNamed:@"selectedcell.png"];
UIImageView *selectedCell = [[UIImageView alloc] initWithImage:selectedGradient];
selectedCell.contentMode = UIViewContentModeScaleToFill;
cell.selectedBackgroundView = selectedCell;
[tableView setBackgroundColor:[UIColor clearColor]];
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,抱歉,我应该回来回答这个问题!
首先确保您的请求不会超过 Twitter API 请求限制,我只是在重新加载表视图时。
修复该错误后,我继续重置密钥,向 Twitter API 写入了使用 xAuth 的请求“只需要几天时间即可被接受”,此后我就没有遇到任何问题。
我没有使用我的密钥,直到我收到 Twitter 发来的信,说我被接受了,从那以后我没有遇到任何问题。
我希望这会有所帮助,因为我知道这会变得多么令人沮丧!
祝你好运!
Yes Sorry I should have came back and answered this!
First make sure your requests are not going over Twitters API request Limit, I was simply while reloading my table view.
After Fixing that mistake I the proceeded to reset my keys wrote a request to Twitter API to use xAuth "it only takes a few days to be accepted" then I have not had any issues since.
I did not use my keys until I received the letter from Twitter saying I was accepted and I have not had any issues since.
I hope this helps because I know how frustrating that can become !
Good Luck!