iOS BoxSDK 返回共享链接为 nil

8
我们需要创建一个文件的共享链接,然后检索该链接,以便我们可以在我们的应用程序中显示它。我们能够为特定文件创建共享链接(我们可以在Web上的Box账户中看到它),但是我们无法通过API检索sharedLink。虽然isShared方法返回YES,但它始终为空。
从BoxObject.h的头文件中,我们发现这两种方法提供了有关项目共享状态的所需信息。
@protocol BoxObject
// ...


// Information about the shared state of the item
@property (readonly, getter = isShared) BOOL shared;
@property (readonly) NSString *sharedLink;

//...
@end

这是我们创建共享链接的方法。
  1. 找到我们想要分享的BoxFile,称其为photo对象。在调用shareWithPassword:message:emails:callbacks:方法之前,[photo isShared]返回NO。
  2. 我们调用[photo shareWithPassword:@"" message:@"" emails:[NSArray arrayWithObject:@""] callbacks:^(id<BoxOperationCallbacks> on1){...}];
  3. 在on1.after内部,我们检查response == BoxCallbackResponseSuccessful,然后调用[photo updateWithCallbacks:^(id on2){..}]
  4. 在on2.after内部,我们检查response == BoxCallbackResponseSuccessful
  5. 在成功响应时,[photo isShared]返回YES,但[photo sharedLink]返回nil

如果我们在Web上检查,可以看到文件实际上是已经共享的,但我们无法从Box SDK中检索共享链接。

是否有人遇到了同样的问题?


如果您能在此过程中发布HTTP流量,将会非常有帮助。这些信息将有助于将问题隔离到Box发送给您的数据或iOS SDK解释数据的方式之一。如果您正在使用Mac,可以使用类似HTTPScoop的工具来捕获流量。 - John Hoerr
已经尝试使用Wireshark了。请求通过HTTPS进行,并不知道是否有一种方法可以强制使用HTTP。 - dtrsan
HTTPScoop提供了一些信息,告诉你如何绕过这个问题。 - John Hoerr
3个回答

1

根据已经发布的代码和在github 这里找到的信息,这对我很有效。

- (void) getShareableLinkForFileId:(NSString *)fileId 
{
    BoxFileBlock fileSuccess = ^(BoxFile *file) {
            NSDictionary *fileInfo = file.rawResponseJSON;
            if (![fileInfo[@"shared_link"] isEqual:[NSNull null]]) { 
            NSDictionary *linkData = fileInfo[@"shared_link"];
            //Do something with the link
        } else {
            // failure
        }
    };
    BoxAPIJSONFailureBlock failure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {
         //Handle the failure 
    };

    BoxFilesRequestBuilder *builder = [[BoxFilesRequestBuilder alloc] init];
    BoxSharedObjectBuilder *sharedBuilder = [[BoxSharedObjectBuilder alloc] init];
    sharedBuilder.access = BoxAPISharedObjectAccessOpen;
    builder.sharedLink = sharedBuilder;
    [[BoxSDK sharedSDK].filesManager editFileWithID:fileId requestBuilder:builder success:fileSuccess failure:failure];
}

0

我通过刷新文件夹本身成功获取了共享链接。这是我编写的代码:

[boxFile shareWithPassword:@"" message:@"" emails:@[ @"" ] callbacks:^(id<BoxOperationCallbacks> on) {
    on.after(^(BoxCallbackResponse response) {
        if (response == BoxCallbackResponseSuccessful) {
            [self.rootFolder updateWithCallbacks:^(id<BoxOperationCallbacks> on) {
                on.after(^(BoxCallbackResponse response) {
                    BoxFile *updatedBoxFile = (BoxFile*)[self.rootFolder.children objectAtIndex:self.selectedIndexPath.row];
                    NSString *fileName = updatedBoxFile.name;
                    NSString *shareLink = updatedBoxFile.sharedLink;

                    NSLog(@"%@ [%@]: %@", fileName, updatedBoxFile.isShared ? @"YES" : @"NO", shareLink);
                });
            }];
        } else {
            [BoxErrorHandler presentErrorAlertViewForResponse:response];
        }
    });
}];

这是使用旧的v1 API。不确定在更新的v2中是否有更改。


0

您可以通过使用Box V2编辑其信息来创建共享链接:

 Box2FolderBlock folderSuccess = ^(Box2Folder *folder) {
    if (![[folder sharedLink] isEqual:[NSNull null]]) {
        NSString *sharedUrl = [[folder sharedLink] objectForKey:Box2APIObjectKeyURL];
    } else {
        // failure
    }
 };

 Box2FileBlock fileSuccess = ^(Box2File *file) {
    if (![[file sharedLink] isEqual:[NSNull null]]) {
       NSString *sharedUrl = [[file sharedLink] objectForKey:Box2APIObjectKeyURL];
    } else {
       // failure
    }
 };

 Box2APIJSONFailureBlock failure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary) {
 };

BoxSharedObjectBuilder *sharedLinkObject = [[BoxSharedObjectBuilder alloc] init];
sharedLinkObject.access = BoxAPISharedObjectAccessOpen;

BoxAPIJSONOperation *operation;

if (isFile == NO) {

    sharedLinkObject.canPreview = BoxAPISharedObjectPermissionStateEnabled;

    BoxFoldersRequestBuilder *requestBuilder = [[BoxFoldersRequestBuilder alloc] init];
    requestBuilder.sharedLink = sharedLinkObject;

    operation = [boxSDK.foldersManager editFolderWithID:fileOrFolderId requestBuilder:requestBuilder success:folderSuccess failure:failure];

} else {

    sharedLinkObject.canDownload = BoxAPISharedObjectPermissionStateEnabled;

    BoxFilesRequestBuilder *requestBuilder = [[BoxFilesRequestBuilder alloc] init];
    requestBuilder.sharedLink = sharedLinkObject;

    operation = [boxSDK.filesManager editFileWithID:fileOrFolderId requestBuilder:requestBuilder success:fileSuccess failure:failure];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接