我想从iOS发布活动到Strava。
Strava文档(http://strava.github.io/api/v3/uploads/#post-file)具有curl示例,如下所示:
示例请求
$ curl -X POST https://www.strava.com/api/v3/uploads \
-F access_token=83ebeabdec09f6670863766f792ead24d61fe3f9 \
-F activity_type=ride \
-F [email protected] \
-F data_type=fit
在这种情况下,文件test.fit是要发布的活动。
我正在尝试使用AFNetworking异步发布。我有以下测试代码:
NSURL *url = [NSURL URLWithString:@"https://www.strava.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *fileData = [NSData dataWithContentsOfFile:filename];
AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:kStravaTokenStored];
NSString *accessToken = credential.accessToken;
NSDictionary *parameters = @{@"access_token": accessToken, @"activity_type" : @"ride",@"data_type" : @"fit", @"name" : @"Test", @"stationary" : @"1" };
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/api/v3/uploads" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:fileData name:@"Test" fileName:@"Test.fit" mimeType:@"application/octet-stream"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"succss %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure %@ \n\n %@", error, operation);
}];
[httpClient enqueueHTTPRequestOperation:operation];
目前,我看到以下错误:
错误域= AFNetworkingErrorDomain代码= -1011“(200-299)中的预期状态代码,得到400” UserInfo = 0x9b62300 {NSLocalizedRecoverySuggestion = {“ message”:“ Bad Request”,“ errors”:[{“ resource”:“ Upload “,” field“:”数据“,”代码“:”空“}]},
有人知道我在这里想念什么吗?
谢谢蚂蚁
它一定要是
[formData appendPartWithFileData:fileData name:@"file" fileName:@"Test.fit" mimeType:@"application/octet-stream"]
或不?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句