Ken*_*999 5 google-sheets ios google-oauth
我有一个iOS 11应用程序,目前可以查询任何Google表格电子表格,以获取任何命名工作表(选项卡)中的所有数据,假设电子表格是公开的(即,不需要身份验证或授权).这是我当前代码的简化版本,可以正常工作:
-(void)fetchSheet
{
if (self.service == nil)
{
self.service = [[GTLRSheetsService alloc] init];
self.service.APIKey = @"MY_API_KEY";
}
// Build the query
NSString *range = @"MainSheet!A1:E";
GTLRSheetsQuery_SpreadsheetsValuesGet *query = [GTLRSheetsQuery_SpreadsheetsValuesGet queryWithSpreadsheetId:self.spreadsheetId range:range];
// Execute the query
[self.service executeQuery:query delegate:self didFinishSelector:@selector(parseSheetWithTicket:finishedWithObject:error:)];
} // fetchSheet
- (void)parseSheetWithTicket:(GTLRServiceTicket *)ticket finishedWithObject:(GTLRSheets_ValueRange *)result error:(NSError *)error
{
// Check error, process as desired
} // parseSheetWithTicket:finishedWithObject:error:
Run Code Online (Sandbox Code Playgroud)
现在,我想做出改变,使得纸张的所有者可以限制片如果需要,只是我的应用程序,但我不希望用户需要一个谷歌帐户,或登录谷歌.我的应用程序应该能够在没有用户参与的情况下阅读电子表格.任何人都可以通过共享MyApp@gmail.com用户(我的应用知道的用户)的只读访问权限,让电子表格与我的应用一起使用.我能找到的最好的想法是如何在没有用户干预的情况下授权应用程序(网络或安装)?这涉及使用Google OAuthPlayground进行初始身份验证,然后使用授权令牌和刷新令牌结束.(当然,我正在使用GTLRSheetsService 服务的,而不是在该网页上显示.驱动服务)的想法是,我的应用程序可以在未来任何时刻使用刷新标记,和谷歌客户端库将使用刷新令牌来获取新的授权令牌,然后进行请求.
我意识到这种方法存在安全隐患.假设我的应用程序将通过iCloud或我托管的服务器获取刷新令牌.(如果我需要更改刷新令牌,这将特别方便.)现在我只是想让它与硬编码的刷新令牌一起工作.
我按照该页面上接受的答案中的说明进行操作,最后得到了刷新令牌(以及客户端ID等).但现在我不知道如何使用它.经过多次搜索,我发现了一些使用的代码GTMOAuth2Authentication,但我想我应该使用GTMAppAuth(对吧?).这是我的新版本fetchSheet:
-(void)fetchSheet
{
if (self.service == nil)
{
self.service = [[GTLRSheetsService alloc] init];
// note that I am no longer setting service.APIKey
// Create GTMOAuth2Authentication object to hold the refresh_token
GTMOAuth2Authentication *auth = [[GTMOAuth2Authentication alloc] init];
auth.clientID = @"MY_CLIENT_ID";
auth.clientSecret = @"MY_CLIENT_SECRET";
auth.userEmail = @"MyApp@gmail.com";
auth.userID = @"myapp";
auth.refreshToken = @"MY_REFRESH_TOKEN";
// Save and use
self.googleAuth = auth;
self.service.authorizer = self.googleAuth;
}
// Build the query
NSString *range = @"MainSheet!A1:E";
GTLRSheetsQuery_SpreadsheetsValuesGet *query = [GTLRSheetsQuery_SpreadsheetsValuesGet queryWithSpreadsheetId:self.spreadsheetId range:range];
// Execute the query
[self.service executeQuery:query delegate:self didFinishSelector:@selector(parseAboutSheetWithTicket:finishedWithObject:error:)];
} // fetchSheet
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,它失败并在调试输出中打印"开始获取需要带URL的请求".它指的是什么网址,为什么我以前不需要设置一个网址?
我使用也试过GTMAppAuthFetcherAuthorization(用OIDServiceConfiguration,OIDAuthorizationRequest和OIDAuthState对象),而不是GTMOAuth2Authentication后来我得到了错误"无法打开Safari浏览器",这表明它试图使用标准的谷歌用户认证和授权的东西.
有谁知道如何使这项工作?如果我正在遵循的方法应该工作,那么我假设我只需要创建一个实现GTMFetcherAuthorizationProtocol和保存刷新令牌的对象.它看起来在概念上很简单,但该页面上的过程并没有描述如何实际使用刷新令牌(以及设置还需要什么).关于使用其他库似乎还有很多其他的东西,但我想继续使用GTLRSheetsService(因为它允许我使用特定的结果类GTLRSheets_ValueRange).
| 归档时间: |
|
| 查看次数: |
197 次 |
| 最近记录: |