【问题标题】:My app is asking for permission to “Have offline access”, why?我的应用要求“离线访问”权限,为什么?
【发布时间】:2015-11-18 16:08:47
【问题描述】:

我的应用请求“离线访问”权限,为什么?这是最奇怪的事情。我做了一些搜索,并没有真正找到任何有效的方法。我已经尝试将这些用于范围:

https://www.googleapis.com/auth/plus.profile.emails.read 
https://www.googleapis.com/auth/plus.login

这似乎没有帮助。

以下是屏幕截图和我的一些代码,可帮助您了解发生了什么:

我的一些代码:

    #import "ViewController.h"

NSString *callbakc =  @"http://localhost/";
NSString *client_id = @“CLIENT ID“;
NSString *scope = @"https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile+https://www.google.com/reader/api/0/subscription";
NSString *secret = @“SECRET”;
NSString *visibleactions = @"http://schemas.google.com/AddActivity";

@interface ViewController () {
NSString *authAccessToken;
UIAlertController *alertController;
}

@property (strong, nonatomic) NSMutableData *receivedData;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
[super viewDidLoad];


NSString *url = [NSString stringWithFormat:@"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=%@&redirect_uri=%@&scope=%@&data-requestvisibleactions=%@",client_id,callbakc,scope,visibleactions];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];

[_webView loadRequest:request];
}



#pragma mark - WebView Delegate

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

[self performSelector:@selector(progressDelay:) withObject:nil afterDelay:0.0];
if ([[[request URL] host] isEqualToString:@"localhost"]) {

    // Extract oauth_verifier from URL query
    NSString* verifier = nil;
    NSArray* urlParams = [[[request URL] query] componentsSeparatedByString:@"&"];
    for (NSString* param in urlParams) {
        if (![param isEqualToString:@"error=access_denied"]) {
            NSArray* keyValue = [param componentsSeparatedByString:@"="];
            NSString* key = [keyValue objectAtIndex:0];
            if ([key isEqualToString:@"code"]) {
                verifier = [keyValue objectAtIndex:1];
//                    NSLog(@"verifier %@",verifier);
                break;
            }
        }
        else {
            [self.navigationController popViewControllerAnimated:NO];
        }
    }

    if (!verifier==0) {
        [self showAlertViewWithTitle:@"" message:@"Please wait" okAction:NO];

        NSString *data = [NSString stringWithFormat:@"code=%@&client_id=%@&client_secret=%@&redirect_uri=%@&grant_type=authorization_code", verifier,client_id,secret,callbakc];
        NSString *url = [NSString stringWithFormat:@"https://accounts.google.com/o/oauth2/token"];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];

        [request setHTTPShouldHandleCookies:NO];

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        NSLog(@"Connection: %@", theConnection);

        self.receivedData = [[NSMutableData alloc] init];
    }
    else {
        // cancel button click
        NSLog(@"not Verified!!");
    }

    return NO;
}
return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
// show progress
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[alertController dismissViewControllerAnimated:YES completion:nil];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

if (error.code==102) //Frame load interrupted
    return;

[alertController dismissViewControllerAnimated:YES completion:nil];
[self showAlertViewWithTitle:@"Error" message:[error localizedDescription] okAction:YES];
}

#pragma mark - NSURLConnection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[self showAlertViewWithTitle:@"Error" message:[NSString stringWithFormat:@"%@", error] okAction:YES];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString *response = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];

NSData *data = [response dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *tokenData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

if ([tokenData objectForKey:@"access_token"]) {
    authAccessToken = [tokenData objectForKey:@"access_token"];
    [self getUserInfo:authAccessToken];
}
else {
    [alertController dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"RESULT: %@", tokenData);
    [self showAlertViewWithTitle:[tokenData objectForKey:@"name"] message:[NSString stringWithFormat:@"%@", tokenData] okAction:YES];

    // Flush all cached data
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

}

#pragma mark - Private Method Implementation

-(void)getUserInfo:(NSString *)token {
NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",token];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
[request setHTTPShouldHandleCookies:NO];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection: %@", theConnection);

self.receivedData = [[NSMutableData alloc] init];

}

-(void)progressDelay:(id)sender {
// Dismiss progress
}


@end

任何帮助将不胜感激!

谢谢

【问题讨论】:

  • 你可以查看这个答案:stackoverflow.com/questions/21405274/…
  • 如何将此令牌作为 url 参数提供? accounts.google.com/o/oauth2/revoke?token={token}
  • 这个令牌是你的:authAccessToken。您使用此令牌获取用户信息。 ` [self getUserInfo:authAccessToken];` 我不确定,但我想是的 ;)
  • 首先我去这里 (security.google.com/settings/u/1/security/permissions) 并删除应用程序的访问权限。然后我删除并重新安装该应用程序,它会正确请求权限。然后,我删除该应用程序并重新安装它。这次它要求离线访问。是因为我有一个开放的 AccessToken 吗?

标签: ios objective-c oauth google-signin


【解决方案1】:

这是来自https://stackoverflow.com/questions/32210920/why-is-my-app-asking-for-permission-to-have-offline-access?answertab=oldest#tab-top

这是正常行为,当用户授予 已获得许可。

基本上,除非您真的不想要,否则无需担心 出现,在这种情况下,您需要取消对用户旧令牌的身份验证 在请求新的之前。

我不太确定怎么做,因为我以前没有这样做过,但是在您授权新令牌之前,您需要取消对旧令牌的授权。

您需要修改-(void)getUserInfo:(NSString *)token 方法。

【讨论】:

    【解决方案2】:

    出于某种我不知道的原因。电子邮件范围弹出

    Have offline access
    

    如果您想删除具有脱机访问权限,请删除电子邮件范围。就我个人而言,我认为这是导致用户要求您访问电子邮件但被提示进行离线访问的失误。从技术上讲,所有返回刷新令牌的 OAuth2 都会提供离线访问权限,因此应始终告知用户您正在获得离线访问权限,但事实并非如此。

    【讨论】:

    • 我删除了电子邮件范围 (googleapis.com/auth/userinfo.email),它仍然提示拥有离线访问权限。 :/ 知道为什么吗?
    • 首先我去这里 (security.google.com/settings/u/1/security/permissions) 并删除应用程序的访问权限。然后我删除并重新安装该应用程序,它会正确请求权限。然后,我删除该应用程序并重新安装它。这次它要求离线访问。是因为我有一个开放的 AccessToken 吗?
    猜你喜欢
    • 2014-02-01
    • 2015-04-20
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多