简体   繁体   English

DocuSign错误下载文件

[英]DocuSign error downloading document

I'm trying to implement this example link of a DocuSign service that download an envelope document, but when the execution arrive to this line in step 3: 我正在尝试实现下载信封文件的DocuSign服务的此示例链接 ,但是当执行在步骤3中到达此行时:

NSMutableString *jsonResponse = [[NSMutableString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];

jsonResponse is nil and when I open the document, it has a lot of strange characters and I can't read it. jsonResponse nil ,当我打开文档时,它有很多奇怪的字符,我看不懂。

My code for downloadind is the following: 我的downloadind代码如下:

NSString *url = @"https://demo.docusign.net/restapi/v2/accounts/373577/envelopes/08016140-e4dc-4697-8146-f8ce801abf92/documents/1";

NSMutableURLRequest *documentsRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

[documentsRequest setHTTPMethod:@"GET"];
[documentsRequest setURL:[NSURL URLWithString:url]];
[documentsRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[documentsRequest setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"];

NSError *error1 = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;

NSData *oResponseData = [NSURLConnection sendSynchronousRequest:documentsRequest returningResponse:&responseCode error:&error1];

NSMutableString *jsonResponse = [[NSMutableString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];

if([responseCode statusCode] != 200){
    NSLog(@"Error sending %@ request to %@\nHTTP status code = %i", [documentsRequest HTTPMethod], url, [responseCode statusCode]);
    NSLog( @"Response = %@", jsonResponse );
    return;
}

// download the document to the same directory as this app
NSString *appDirectory = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];

NSMutableString *filePath = [NSMutableString stringWithFormat:@"%@/%@", appDirectory, @"eee"];

[oResponseData writeToFile:filePath atomically:YES];
NSLog(@"Envelope document - %@ - has been downloaded to %@\n", @"eee", filePath);

If no data is coming back from 如果没有数据返回

NSMutableString *jsonResponse = [[NSMutableString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];

then that means oResponseData is most likely nil which means the URL is not being constructed correctly for each envelope document. 那么这意味着oResponseData很可能为nil,这意味着每个信封文档的URL构造都不正确。 Are you doing step 2 in the sample that you linked to , where it first retrieves the envelopeDocuments information about the envelope, then it dynamically builds each unique document uri in preparation of downloading each one. 您是否在链接到样本中执行第2步,在该步骤中,它首先检索有关envelopeDocuments信息,然后动态地构建每个唯一的文件uri,以准备下载每个文件。

Instead of hardcoding your URL you should dynamically retrieve the envelopeDocuments data, then build each document uri dynamically then your response data will not be nil. 而不是对URL进行硬编码,您应该动态地检索envelopeDocuments数据,然后动态地构建每个文档uri,这样您的响应数据就不会为零。 Here's the full working sample: 这是完整的工作示例:

- (void)getDocumentInfoAndDownloadDocuments
{

    // Enter your info:
    NSString *email = @"<#email#>";
    NSString *password = @"<#password#>";
    NSString *integratorKey = @"<#integratorKey#>";

    // need to copy a valid envelopeId from your account
    NSString *envelopeId = @"<#envelopeId#>";

    ///////////////////////////////////////////////////////////////////////////////////////
    // STEP 1 - Login (retrieves accountId and baseUrl)
    ///////////////////////////////////////////////////////////////////////////////////////

    NSString *loginURL = @"https://demo.docusign.net/restapi/v2/login_information";

    NSMutableURLRequest *loginRequest = [[NSMutableURLRequest alloc] init];
    [loginRequest setHTTPMethod:@"GET"];
    [loginRequest setURL:[NSURL URLWithString:loginURL]];

    // set JSON formatted X-DocuSign-Authentication header (XML also accepted)
    NSDictionary *authenticationHeader = @{@"Username": email, @"Password" : password, @"IntegratorKey" : integratorKey};

    // jsonStringFromObject() function defined further below...
    [loginRequest setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"];

    // also set the Content-Type header (other accepted type is application/xml)
    [loginRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    [NSURLConnection sendAsynchronousRequest:loginRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *loginResponse, NSData *loginData, NSError *loginError) {

        if (loginError) {   // succesful GET returns status 200
            NSLog(@"Error sending request %@. Got Response %@ Error is: %@", loginRequest, loginResponse, loginError);
            return;
        }

        // we use NSJSONSerialization to parse the JSON formatted response
        NSError *jsonError = nil;
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:loginData options:kNilOptions error:&jsonError];
        NSArray *loginArray = responseDictionary[@"loginAccounts"];

        // parse the accountId and baseUrl from the response (other data included)
        NSString *accountId = loginArray[0][@"accountId"];
        NSString *baseUrl = loginArray[0][@"baseUrl"];

        //--- display results
        NSLog(@"\naccountId = %@\nbaseUrl = %@\n", accountId, baseUrl);

        ///////////////////////////////////////////////////////////////////////////////////////
        // STEP 2 - Get Document Info for specified envelope
        ///////////////////////////////////////////////////////////////////////////////////////

        // append /envelopes/{envelopeId}/documents URI to baseUrl and use as endpoint for next request
        NSString *documentsURL = [NSMutableString stringWithFormat:@"%@/envelopes/%@/documents", baseUrl, envelopeId];

        NSMutableURLRequest *documentsRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:documentsURL]];
        [documentsRequest setHTTPMethod:@"GET"];

        [documentsRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [documentsRequest setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"];

        [NSURLConnection sendAsynchronousRequest:documentsRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *documentsResponse, NSData *documentsData, NSError *documentsError) {
            NSError *documentsJSONError = nil;
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:documentsData options:kNilOptions error:&documentsJSONError];
            if (documentsError){
                NSLog(@"Error sending request: %@. Got response: %@", documentsRequest, documentsResponse);
                NSLog( @"Response = %@", documentsResponse );
                return;
            }
            NSLog( @"Documents info for envelope is:\n%@", jsonResponse);
            NSError *jsonError = nil;
            NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:documentsData options:kNilOptions error:&jsonError];
            // grab documents info for the next step...
            NSArray *documentsArray = responseDictionary[@"envelopeDocuments"];

            ///////////////////////////////////////////////////////////////////////////////////////
            // STEP 3 - Download each envelope document
            ///////////////////////////////////////////////////////////////////////////////////////

            NSMutableString *docUri;
            NSMutableString *docName;
            NSMutableString *docURL;

            // loop through each document uri and download each doc (including the envelope's certificate)
            for (int i = 0; i < [documentsArray count]; i++)
            {
                docUri = [documentsArray[i] objectForKey:@"uri"];
                docName = [documentsArray[i] objectForKey:@"name"];
                docURL = [NSMutableString stringWithFormat: @"%@/%@", baseUrl, docUri];

                [documentsRequest setHTTPMethod:@"GET"];
                [documentsRequest setURL:[NSURL URLWithString:docURL]];
                [documentsRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                [documentsRequest setValue:[self jsonStringFromObject:authenticationHeader] forHTTPHeaderField:@"X-DocuSign-Authentication"];

                NSError *error = [[NSError alloc] init];
                NSHTTPURLResponse *responseCode = nil;
                NSData *oResponseData = [NSURLConnection sendSynchronousRequest:documentsRequest returningResponse:&responseCode error:&error];
                NSMutableString *jsonResponse = [[NSMutableString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
                if([responseCode statusCode] != 200){
                    NSLog(@"Error sending %@ request to %@\nHTTP status code = %i", [documentsRequest HTTPMethod], docURL, [responseCode statusCode]);
                    NSLog( @"Response = %@", jsonResponse );
                    return;
                }

                // download the document to the same directory as this app
                NSString *appDirectory = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
                NSMutableString *filePath = [NSMutableString stringWithFormat:@"%@/%@", appDirectory, docName];
                [oResponseData writeToFile:filePath atomically:YES];
                NSLog(@"Envelope document - %@ - has been downloaded to %@\n", docName, filePath);
            } // end for
        }];
    }];
}

- (NSString *)jsonStringFromObject:(id)object {
    NSString *string = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:object options:0 error:nil] encoding:NSUTF8StringEncoding];
    return string;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM