gpt4 book ai didi

ios - 通过oauth将图像发送到twitter

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:39 24 4
gpt4 key购买 nike

我可以使用 oauth 成功登录 Twitter。现在我需要发布带有状态的图像。为此,我实现了以下......

-(void)shareontw{
NSString *postUrl =@"https://api.twitter.com/1.1/statuses/update_with_media.json";
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postUrl]];
[req setValue:[oAuth oAuthHeaderForMethod:@"POST" andUrl:postUrl andParams:nil] forHTTPHeaderField:@"Authorization"];
[req setHTTPMethod:@"POST"];
NSString *boundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
boundary];
[req addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
NSMutableData *myRequestData = [NSMutableData data];
NSData *imageData = UIImageJPEGRepresentation(globalimage, 0.8);

[myRequestData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Disposition: form-data; name=\"media\"; filename=\"dummy.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// add it to body
[myRequestData appendData:imageData];
[myRequestData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Content-Disposition: form-data; name=\"message\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData appendData:[@"Success\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[myRequestData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:myRequestData];
NSHTTPURLResponse *response;
NSError *error = nil;
NSString *responseString = [[[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error] encoding:NSUTF8StringEncoding] autorelease];

if (error) {
NSLog(@"Error from NSURLConnection: %@", error);
}
NSLog(@"Got HTTP status code from Twitter after posting profile image: %d", [response statusCode]);
NSLog(@"Response string: %@", responseString);}

但它给我错误:响应字符串:{"errors":[{"message":"Bad Authentication data","code":215}]}

我找不到我在这里做错的问题。如果有人有想法,请帮助我。

提前致谢。

最佳答案

感谢你上面的代码工作正常,为我节省了很多时间。我只更改一行代码

这一行 NSData *imageData = UIImageJPEGRepresentation(globalimage, 0.8);

替换为

NSData *imageData = UIImageJPEGRepresentation(_imgView.image, 0.8);

它正在工作:)

关于ios - 通过oauth将图像发送到twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20042700/

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