我正在尝试根据Twitter https://dev.twitter.com/rest/reference/post/account/update_profile_image上的文档发布新的个人资料图片,这是我的代码:
NSData *jpeg = UIImageJPEGRepresentation(img, 0.9);
NSString *base64 = [jpeg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/update_profile_image.json"];
NSDictionary *params = @{@"image" : base64
};
SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];
[request setAccount:self.twitterAccount];
[request performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse,
NSError *error) {
...
}
但网址回复是:
errors = (
{
code = 215;
message = "Bad Authentication data.";
}
);
我做错了什么?我的ACAccount
有效(我刚尝试在Settings.app中退出和注销)。
答案 0 :(得分:1)
你的代码似乎很好,但你必须遗漏一些东西。
确保:
requestAccessToAccountsWithType
方法)。授予权限后,您应该会在Twitter设置(位于“设置”应用中)中的“#34;允许这些应用程序使用您的帐户"”中列出您的应用程序。SLRequest
的Twitter帐户有效且不是nil
。鉴于上述条件,我刚才能够成功修改我的个人资料图片。以下是基于您提供的代码的示例代码:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
ACAccount *twitterAccount = [accounts lastObject];
NSData *jpeg = UIImageJPEGRepresentation([UIImage imageNamed:@"photo.jpg"], 0.9);
NSString *base64 = [jpeg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/account/update_profile_image.json"]
parameters:@{@"image" : base64}];
[request setAccount:twitterAccount];
[request performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse,
NSError *error)
{
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil]);
}];
}
}];