ios - Twitter - 关注特定用户
问题描述
我正在开发 iOS 应用程序,用户可以从 twitter 登录并关注某些用户。但关注不起作用。
- (void)executeFollowRequestWithComplition:(nonnull SuccessResultBlock)complition {
NSURLSessionConfiguration *defaultSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultSessionConfiguration];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/friendships/create.json"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString *postParams = [NSString stringWithFormat:@"screen_name=%@&follow=true", twitterName];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[postParams dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
ANSPerformBlock(complition, (error == nil), error)
}];
[dataTask resume];
}
解决方案
您的请求中似乎没有任何身份验证信息。
如果您查看 twitter 文档,它会为您提供 curl 示例
--url 'https://api.twitter.com/1.1/friendships/create.json?user_id=USER_ID_TO_FOLLOW&follow=true'
--header 'authorization: OAuth oauth_consumer_key="YOUR_CONSUMER_KEY", oauth_nonce="AUTO_GENERATED_NONCE", oauth_signature="AUTO_GENERATED_SIGNATURE", oauth_signature_method="HMAC-SHA1", oauth_timestamp="AUTO_GENERATED_TIMESTAMP", oauth_token="USERS_ACCESS_TOKEN", oauth_version="1.0"'
--header 'content-type: application/json'
您缺少的重要信息在这里
--header 'authorization: OAuth oauth_consumer_key="YOUR_CONSUMER_KEY", oauth_nonce="AUTO_GENERATED_NONCE", oauth_signature="AUTO_GENERATED_SIGNATURE", oauth_signature_method="HMAC-SHA1", oauth_timestamp="AUTO_GENERATED_TIMESTAMP", oauth_token="USERS_ACCESS_TOKEN", oauth_version="1.0"'
您可以通过像这样添加每个标题字段来附加该信息
urlRequest.addValue("oauth_consumer_key", forHTTPHeaderField: "YOUR_CONSUMER_KEY")
推荐阅读
- oracle - 有没有办法在 SQL Developer 中自动运行 select 语句
- android - 发送键值列表对象进行改造
- excel - VBscript - 如果值来自变量,Excel 无法找到
- javascript - onhashchange 无法完全监控哈希变化事件
- javascript - 无法在路线之间导航。VueJS
- spring-boot - WebFlux 新手的烦恼
- go - 用于动态枚举访问的 Protobuffer API
- google-calendar-api - 使用 API 修改 Google 日历重复事件
- go - 为什么我会通过渠道接收其他元素?
- java - 带有 HTML 的 Spring Boot