Twitter - 关注某些用户

问题描述 投票:-2回答:1

我正在开发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];
}
ios objective-c twitterkit
1个回答
0
投票

您的请求中似乎没有任何身份验证信息。

如果你查看twitter文档,它会给你一个卷曲的例子

--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")

© www.soinside.com 2019 - 2024. All rights reserved.