我想在我的iOS应用中与Google云端硬盘集成。
我已经完成了授权代码,我正在获取accessToken,所以我想知道 - 从Google Drive中检索PDF文件的方式去哪里。
我的登录代码:
- (IBAction)signInButtonTapped:(id)sender {
NSURL *issuer = [NSURL URLWithString:kIssuer];
NSURL *redirectURI = [NSURL URLWithString:kRedirectURI];
[self logMessage:@"Fetching configuration for issuer: %@", issuer];
// discovers endpoints
[OIDAuthorizationService discoverServiceConfigurationForIssuer:issuer
completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
if (!configuration) {
[self logMessage:@"Error retrieving discovery document: %@", [error localizedDescription]];
[self setAuthState:nil];
return;
}
[self logMessage:@"Got configuration: %@", configuration];
// builds authentication request
OIDAuthorizationRequest *request =
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:kClientID
scopes:@[OIDScopeOpenID, OIDScopeProfile]
redirectURL:redirectURI
responseType:OIDResponseTypeCode
additionalParameters:nil];
// performs authentication request
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[self logMessage:@"Initiating authorization request with scope: %@", request.scope];
appDelegate.currentAuthorizationFlow =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
presentingViewController:self
callback:^(OIDAuthState *_Nullable authState,
NSError *_Nullable error) {
if (authState) {
[self setAuthState:authState];
[self logMessage:@"Got authorization tokens. Access token: %@", authState.lastTokenResponse.accessToken];
[self logMessage:@"Got authorization tokens. Refresh Access token %@", authState.refreshToken];
} else {
[self logMessage:@"Authorization error: %@", [error localizedDescription]];
[self setAuthState:nil];
}
}];}];}
答案 0 :(得分:1)
代码参考库:https://github.com/google/google-api-objectivec-client-for-rest
执行Google云端硬盘服务请求的方法。
- (GTLRDriveService *)driveService {
static GTLRDriveService *service;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
service = [[GTLRDriveService alloc] init];
// Turn on the library's shouldFetchNextPages feature to ensure that all items
// are fetched. This applies to queries which return an object derived from
// GTLRCollectionObject.
service.shouldFetchNextPages = YES;
// Have the service object set tickets to retry temporary error conditions
// automatically
service.retryEnabled = YES;
});
return service;
}
在Google身份验证之后,使用以下行授权driveService:
在你的情况下,
if (authState) {
// Creates a GTMAppAuthFetcherAuthorization object for authorizing requests.
GTMAppAuthFetcherAuthorization *gtmAuthorization =
[[GTMAppAuthFetcherAuthorization alloc] initWithAuthState:authState];
// Sets the authorizer on the GTLRYouTubeService object so API calls will be authenticated.
strongSelf.driveService.authorizer = gtmAuthorization;
// Serializes authorization to keychain in GTMAppAuth format.
[GTMAppAuthFetcherAuthorization saveAuthorization:gtmAuthorization
toKeychainForName:kKeychainItemName];
// Your further code goes here
//
[self fetchFileList];
}
然后,您可以使用以下方法从Google云端硬盘获取文件:
- (void)fetchFileList {
__block GTLRDrive_FileList *fileListNew = nil;
GTLRDriveService *service = self.driveService;
GTLRDriveQuery_FilesList *query = [GTLRDriveQuery_FilesList query];
// Because GTLRDrive_FileList is derived from GTLCollectionObject and the service
// property shouldFetchNextPages is enabled, this may do multiple fetches to
// retrieve all items in the file list.
// Google APIs typically allow the fields returned to be limited by the "fields" property.
// The Drive API uses the "fields" property differently by not sending most of the requested
// resource's fields unless they are explicitly specified.
query.fields = @"kind,nextPageToken,files(mimeType,id,kind,name,webViewLink,thumbnailLink,trashed)";
GTLRServiceTicket *fileListTicket;
fileListTicket = [service executeQuery:query
completionHandler:^(GTLRServiceTicket *callbackTicket,
GTLRDrive_FileList *fileList,
NSError *callbackError) {
// Callback
fileListNew = fileList;
}];
}
从参考库中尝试DriveSample,在项目中包含GTLRDrive文件,您就可以使用上述方法了。
要使GTMAppAuthFetcherAuthorization正常工作,您必须包含“GTMAppAuth”窗格或在项目中手动包含这些文件。
实际上,上述方法是从引用库的DriveSample示例中复制的,此示例适用于Drive请求。