2014-03-05 41 views
0

所以我有一個應用程序需要使用OAuth2訪問Google的數據。我有方法刷新存儲的數據並將新帳戶添加到應用程序中。我一直在使用兩個帳戶進行測試。使用一個帳戶登錄我沒有任何問題,使用該帳戶刷新也沒問題(首先)。在同時簽署我的第二個帳戶並同時刷新兩組數據方面,我沒有任何問題。然而,在長時間的缺席之後,我回來嘗試刷新一個或兩個帳戶,並崩潰。從我的日誌中我推斷出令牌不再有用,服務器也不再給我回數據。每次我發送請求時,首先使用刷新令牌刷新我的訪問令牌,這些令牌保存在手機內存中。Google OAuth令牌在一段時間後停止工作(iOS 7應用)

Q1:我是否正確評估了問題,即我的令牌有問題嗎?

問題2:刷新令牌在給定時間段後過期嗎?

如果是Q3:我該如何解決這個問題?

這裏是我的代碼段,從令牌刷新的數據請求:

NSString *post = [NSString stringWithFormat:@"client_id=%@&client_secret=%@&refresh_token=%@&grant_type=refresh_token", mAuth.clientID, mAuth.clientSecret, [self.defaults objectForKey:[NSString stringWithFormat:@"refresh_token %i", i]]]; 
NSLog(@"AT - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"access_token %i", i]]); 
NSLog(@"TT - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"token_type %i", i]]); 
NSLog(@"ET - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"expires_in %i", i]]); 
NSLog(@"RT - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"refresh_token %i", i]]); 
NSLog(@"%@", post); 
NSMutableData *postData = [NSMutableData data]; 
[postData appendData:[post dataUsingEncoding:NSASCIIStringEncoding]]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://accounts.google.com/o/oauth2/token"]]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 
NSURLResponse *response; 
NSError *error; 
NSData *connection = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSString *tokenString = [[NSString alloc]initWithData:connection encoding:NSUTF8StringEncoding]; 
NSLog(@"%@", tokenString); 
[self complexParseFromString:tokenString forInt:i]; 
NSLog(@"AT - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"access_token %i", i]]); 
NSLog(@"TT - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"token_type %i", i]]); 
NSLog(@"ET - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"expires_in %i", i]]); 
NSLog(@"RT - %@", [self.defaults objectForKey:[NSString stringWithFormat:@"refresh_token %i", i]]); 
NSMutableURLRequest *GETRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true"]]]; 
[GETRequest setHTTPMethod:@"GET"]; 
[GETRequest setValue:@"www.googleapis.com" forHTTPHeaderField:@"Host"]; 
[GETRequest setValue:[NSString stringWithFormat:@"Bearer %@", [self.defaults objectForKey:[NSString stringWithFormat:@"access_token %i", i]]] forHTTPHeaderField:@"Authorization"]; 
connection = [NSURLConnection sendSynchronousRequest:GETRequest returningResponse:&response error:&error]; 
responderString = [[NSString alloc]initWithData:connection encoding:NSUTF8StringEncoding]; 
arrayChannel = [[NSMutableArray alloc]initWithArray:[self simpleParseFromString:responderString]]; 
GETRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=%@", [arrayChannel objectAtIndex:[arrayChannel indexOfObject:@"uploads"] + 1]]]]; 
[GETRequest setHTTPMethod:@"GET"]; 
[GETRequest setValue:@"www.googleapis.com" forHTTPHeaderField:@"Host"]; 
[GETRequest setValue:[NSString stringWithFormat:@"Bearer %@", [self.defaults objectForKey:[NSString stringWithFormat:@"access_token %i", i]]] forHTTPHeaderField:@"Authorization"]; 
connection = [NSURLConnection sendSynchronousRequest:GETRequest returningResponse:&response error:&error]; 
responderString = [[NSString alloc]initWithData:connection encoding:NSUTF8StringEncoding]; 

回答

0

https://developers.google.com/accounts/docs/OAuth2ForDevices

「訪問令牌有一個有限終身如果應用程序需要訪問。然後它可以使用刷新令牌來獲得一個新的訪問令牌(請參閱使用刷新令牌)如果您的應用程序需要這種類型的訪問,那麼它應該存儲刷新令牌供以後使用「。

注意:對於Google,刷新標記不會過期。不過,我也看到過期刷新令牌的oauth提供程序。 AFAIK規範沒有指定。

相關問題