【问题标题】:DocuSign getEnvelopeDocuments invalid value envelopeIdDocuSign getEnvelopeDocuments 无效值信封Id
【发布时间】:2014-03-18 14:50:37
【问题描述】:

我正在objective C 中实现有关获取我在此link 中找到的信封文档的DocuSign 示例,但是当代码执行指令时

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

URL 请求为空,因为URLWithString 失败。我获取的网址是​https://demo.docusign.net/restapi/v2/accounts/373577/envelopes/2513432b-07f4-4117-bb6c-8b5d49606d2d/documents

我也尝试做

[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

但在这种情况下,结果是 Invalid value specified for envelopeid 带有 url ​https://demo.docusign.net/restapi/v2/accounts/373577/envelopes/2513432b-07f4-4117-bb6c-8b5d49606d2d%E2%80%8B/documents

【问题讨论】:

  • 我刚刚测试并运行了 linked to here 的 Objective-C 演练,在我在顶部插入我的信息(包括我帐户中的有效信封Id)后,它对我来说很好。在失败的行之前,您对代码进行了哪些更改?一定是您更改了某些内容,或者您​​没有使用有效的信封Id。
  • 如果您没有更改任何内容,那么我建议您发布您正在使用的完整示例,以便我们对其进行检查和测试...
  • 感谢您的 cmets,但我刚刚做得很好,而且效果很好。我改变了获取信封的方式。
  • 好的,在这种情况下,您能否接受以下答案,其中包含完整的工作 API 示例?谢谢,-Ergin

标签: ios objective-c xcode5 nsurl docusignapi


【解决方案1】:

用于获取信封文档信息和下载文档的 DocuSign API 演练具有已确认有效的完整工作代码。这是完整的工作示例,您只需输入您的凭据和有效的envelopeId

//
//  API Walkthrough 6 - Get envelope documents info and download documents
//
//  To run this sample:
//      1.  Copy the below code into your iOS project
//      2.  Enter your email, password, integrator key and envelopeId and save
//      3.  Run the code
//

- (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;
}

【讨论】:

  • 好吧,在这个例子中,我在下载文档时遇到了问题。当我下载一个文档时,如果我打开文档,MSWord 会告诉我选择编码并显示奇怪的字符。你能帮我吗?
  • 在 DocuSign 中创建信封后,信封的文档将转换为 PDF。因此,当您下载完成的文档时,它们是 PDF - 尝试使用 PDF 查看器而不是 Microsoft Word 打开它...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多