gpt4 book ai didi

ios - 如何在 iOS 中使用 Twitter API 1.1 获取 Token

转载 作者:行者123 更新时间:2023-11-28 22:20:55 34 4
gpt4 key购买 nike

我正在使用“STTwitter”从下面的 URL 和请求正文中获取 token

https://dev.twitter.com/docs/api/1.1/post/oauth2/token

带有URL的请求正文代码如下

- (void)verifyCredentialsWithSuccessBlock:(void(^)(NSString *username))successBlock errorBlock:(void(^)(NSError *error))errorBlock {

[self postResource:@"oauth2/token"
baseURLString:@"https://api.twitter.com"
parameters:@{ @"grant_type" : @"client_credentials" }
useBasicAuth:YES
uploadProgressBlock:nil
downloadProgressBlock:nil
successBlock:^(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, id json) {

NSString *tokenType = [json valueForKey:@"token_type"];
if([tokenType isEqualToString:@"bearer"] == NO) {
NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:STTwitterAppOnlyCannotFindBearerTokenInResponse userInfo:@{NSLocalizedDescriptionKey : @"Cannot find bearer token in server response"}];
errorBlock(error);
return;
}

self.bearerToken = [json valueForKey:@"access_token"];

successBlock(_bearerToken);

} errorBlock:^(id request, NSDictionary *requestHeaders, NSDictionary *responseHeaders, NSError *error) {
errorBlock(error);


NSLog(@"ERROR %@",[error description]);

}];
}

为了调用上面的方法我正在做下面的代码

STTwitterAppOnly *twitter1 = [[STTwitterAppOnly alloc] init];
[twitter1 verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

NSLog(@"sd");

[self.twitter getUserTimelineWithScreenName:@"SEREEN_NAME"
successBlock:^(NSArray *statuses) {

NSLog(@"ERROR ::: %@",[statuses description]);

// ...
} errorBlock:^(NSError *error) {
// ...
}];

} errorBlock:^(NSError *error) {
// ...
}];

执行上面的代码时出现以下错误...

**Error Domain=STHTTPRequest Code=99 "Unable to verify your credentials"** 

你能告诉我我在代码中遗漏了什么吗?

我担心的是,我想在不登录 Twitter 的情况下阅读 Twitter 提要。仅在 Twitter API V1.1 中使用“Consumer Key”和“Secret Key”。

谢谢

最佳答案

如果你有ConsumerKey和ConsumerSecret你可以试试

STTwitterAPIWrapper *twitterAPI = [[STTwitterAPIWrapper twitterAPIApplicationOnlyWithConsumerKey:@"Your Consumer Key" consumerSecret:@"Your Consumer Secret"] autorelease];
[twitterAPI verifyCredentialsWithSuccessBlock:^(NSString *username) {
[twitterAPI getUserTimelineWithScreenName:@"your ScreenName" count:25 successBlock:^(NSArray *statuses) {
NSLog(@"Success:%@", statuses);

} errorBlock:^(NSError *error){
NSLog(@"Error : %@",error.description);
}];

} errorBlock:^(NSError *error) {
NSLog(@"Error : %@",error.description);
}];

关于ios - 如何在 iOS 中使用 Twitter API 1.1 获取 Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438263/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com