Objective c iOS谷歌硬盘集成
我想在我的iOS应用程序中与Google Drive集成 我已经完成了授权的代码,我正在取回accessToken,所以我想知道——从Google Drive检索PDF文件的方向 我的登录码:Objective c iOS谷歌硬盘集成,objective-c,google-drive-api,Objective C,Google Drive Api,我想在我的iOS应用程序中与Google Drive集成 我已经完成了授权的代码,我正在取回accessToken,所以我想知道——从Google Drive检索PDF文件的方向 我的登录码: - (IBAction)signInButtonTapped:(id)sender { NSURL *issuer = [NSURL URLWithString:kIssuer]; NSURL *redirectURI = [NSURL URLWithString:kRedirec
- (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];
}
}];}];}
代码参考库:
用于执行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;
}
谷歌认证后,使用以下线路授权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 Drive获取文件:
- (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正常工作,您必须在项目中包含pod“GTMAppAuth”或手动包含文件
实际上,上述方法是从引用库的DriveSample示例复制而来的,该示例对于驱动器请求来说运行良好。您可能需要检查。给出的示例演示了如何使用客户端库下载PDF格式的Google文档。您还可以查看支持的导出MIME类型表,以获得每个Google文档格式的相应MIME类型。有关更详细的信息,您可能需要检查@Sipho Koza,需要将哪个url设置为重定向URI?我被困在这里了,它也需要添加到开发者控制台吗?请帮忙。我已经写了一个有步骤解释的中型博客。看看这个谢谢你的回复。这对我来说非常好。@GauravSingla你知道如何获取特定文件夹的id吗?如何将图像上传到硬盘?
- (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;
}