API を使用するアプリを構築していますが、PHP を使用して動作していますが、xcode で構築する必要があり、「不正な認証ヘッダー」というエラーが返されます。ObjectC コードで見逃したものやデバッグ方法がわかりません.
私のQuery.mでは、これがヘッダーの作成方法です
- (NSData *)multipartFormDataWithBoundary:(NSString *)boundary
{
NSMutableData *body = [NSMutableData data];
// Image data part
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"query[file]\"; filename=\"query.%@\"\r\n", imageType] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/%@\r\n", imageType] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: binary\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:imageData];
[body appendData:[@"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
// Location parts
if (self.location) {
[body appendData:[self textPart:[NSString stringWithFormat:@"%f", location.coordinate.latitude] forKey:@"query[latitude]" boundary:boundary]];
[body appendData:[self textPart:[NSString stringWithFormat:@"%f", location.coordinate.longitude] forKey:@"query[longitude]" boundary:boundary]];
}
// End of boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];
return body;
}
#pragma mark -
#pragma mark Instance methods
- (NSString *)create
{
NSURL *queriesURL = [NSURL URLWithString:@"https://url.com/v4/query"];
NSString *contentType = @"multipart/form-data";
NSString *httpMethod = @"POST";
NSString *boundary = [NSString stringWithFormat:@"%d", arc4random() % 999999];
NSData *contentData = [self multipartFormDataWithBoundary:boundary];
NSString *dateValue = HttpDate();
// request header values
NSString *authorizationValue = [NSString stringWithFormat:@"Authorization: Token %@", accessKey];
NSString *contentTypeValue = [NSString stringWithFormat:@"%@; boundary=%@", contentType, boundary];
// create the request object
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:queriesURL];
[request setHTTPMethod:httpMethod];
[request addValue:contentTypeValue forHTTPHeaderField:@"Content-Type"];
[request addValue:authorizationValue forHTTPHeaderField:@"Authorization"];
[request addValue:dateValue forHTTPHeaderField:@"Date"];
[request setHTTPBody:contentData];
NSLog(@"request: %@", request);
// send the request
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"return data: %@", returnData);
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"return string: %@", returnString);
return returnString;
}
そして、これは私が私の写真で使用しているものです.m
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
if (originalImage) {
NSData *jpegData = UIImageJPEGRepresentation(originalImage, 0.75);
KQuery *query = [[KQuery alloc] initWithImageData:jpegData type:@"jpeg"];
query.secretKey = @"3r4yq0LI";
//query.location = locationManager.location; // might be nil
result = [query create];
NSLog(@"The return string: %@", [query create]);
}
これは実際の PHP クエリです。
$boundary = uniqid();
# Construct the body of the request
$body = image_part($boundary, "image", $file_name, $img);
$body .= "--" . $boundary . "--\r\n";
var_dump($body);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: multipart/form-data; boundary='.$boundary."\r\n" .
'Authorization: Token ' . $query_key,
'content' => $body
)
));
$result = file_get_contents($url, false, $context);
echo "Result: ", $result;
# add image part to a kooaba multipart request
function image_part($boundary, $attr_name, $file_name, $data) {
$str = "--" . $boundary . "\r\n";
$str .= 'Content-Disposition: form-data; name="'. $attr_name .'"; filename="' . $file_name . '"' . "\r\n";
$str .= 'Content-Transfer-Encoding: binary' ."\r\n";
$str .= 'Content-Type: image/jpeg' . "\r\n\r\n";
$str .= $data . "\r\n";
return $str;
}
PHPで使用するものはすべてObjectCにもあると思います...、送信したヘッダーファイルを確認する方法またはこれをデバッグする方法を知っている人はいますか??