1

我正在尝试以这种方式发出 HTTP 请求:

 NSString *urlString = [NSString stringWithFormat:@"https://api.dropbox.com/1/oauth/request_token"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

 oauth_version="1.0"
oauth_signature_method="PLAINTEXT"
oauth_consumer_key="<app-key>"
oauth_signature="<app-secret>&"

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];

//post
[request setHTTPBody:postBody];

//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
    NSLog(@"Response: %@", result);

    //here you get the response

}

我正在尝试使用这些标头发出请求:

Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="<app-key>", oauth_signature="<app-secret>&"

但我不明白怎么做。请帮忙!!

4

3 回答 3

1

在您的情况下,授权只是一个 HTTP 标头。所以这是:

[request addValue:@"OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"<app-key>\", oauth_signature=\"<app-secret>&\"" forHTTPHeaderField: @"Authorization"];

或者:

NSString* oauth_version=@"1.0";
NSString* oauth_signature_method=@"PLAINTEXT";
NSString* oauth_consumer_key=@"<app-key>";
NSString* oauth_signature=@"<app-secret>&";
NSString* authHeader = [NSString stringWithFormat: @"OAuth oauth_version=\"%@\", oauth_signature_method=\"%@\", oauth_consumer_key=\"%@\", oauth_signature=\"%@\"",
    oauth_version, oauth_signature_method, oauth_consumer_key, oauth_signature];
于 2012-07-23T11:37:33.473 回答
0

尝试评论这一切

//NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
//NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(@"Response Code: %d", [urlResponse statusCode]);
//if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
   // NSLog(@"Response: %@", result);

    //here you get the response

//}

并使用它而不是以上所有

 NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];

实现这些委托功能

注意 - self.data 是在标头中声明为数据的 NSMUtableData 对象。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
    [self.data appendData:d];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
                                 message:[error localizedDescription]
                                delegate:nil
                       cancelButtonTitle:NSLocalizedString(@"OK", @"") 
                       otherButtonTitles:nil] autorelease] show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


    NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];

    // Do anything you want with it 
    [responseText release];
}

// Handle basic authentication challenge if needed
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

//I'm using HTTP Digest Authentication in your case it could be different
    NSURLCredential *credential = [NSURLCredential credentialWithUser:HTTP_DIGEST_USER
                                                             password:HTTP_DIGEST_PASSWORD
                                                          persistence:NSURLCredentialPersistenceForSession];
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}

希望它会有所帮助

于 2012-07-23T11:20:22.340 回答
0

当您将错误的包或文档目录路径传递给媒体时,会发生此错误。只需简单地验证您的 HTTP 请求正文的媒体路径。

于 2012-12-07T06:09:46.597 回答