2014-12-24 88 views
0

我在Google日曆API工作。使用刷新訪問令牌給出「請求失敗:未授權(401)」

下面是用於創建日曆代碼

if([self.auth shouldRefreshAccessToken]) { 
    self.auth.accessToken=self.auth.refreshToken; 
} 

NSString *strURL=[NSString stringWithFormat:@"https://www.googleapis.com/calendar/v3/calendars?key=%@&access_token=%@",kGoogleClientID,self.auth.accessToken]; 

NSDictionary *params = @{@"summary":@"1233"}; 

[manager POST:strURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", [operation responseString]); 
    NSDictionary *dict =(NSDictionary*)responseObject; 

    [[NSUserDefaults standardUserDefaults] setObject:[dict objectForKey:@"id"] forKey:KDefaultCalendarID]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 

    //  [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    //  UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"" message:@"Request failed due to server error. Please try after some time." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
    //  [alertView show]; 
}]; 

問題是當使用刷新令牌,我收到「請求失敗:未經授權(401)」 (刷新令牌作爲用戶不應每次需要登錄)

對於用戶第一次授權應用程序後檢索的正常訪問令牌(未過期),它可以正常工作。

下面是運行時的url,它只在刷新的訪問令牌沒有被使用之前工作。

實際URL:https://www.googleapis.com/calendar/v3/calendars?key=1080844328725-t8jo4s3cpqtg9orcp63lkujh1dvsqugq.apps.googleusercontent.com&access_token=1/x4c8cfLrfYamVgkBqSsi85N34wX7O_IxFutis08BGaN90RDknAdJa_sgfheVM0X

回答

0
-(void) setNewAccessToken { 
NSString *clientID=kGoogleClientID; 
NSString *clientSecret=kGoogleClientSecret; 
NSString *strRefreshToken; 

if([self.auth shouldRefreshAccessToken]) { 
    strRefreshToken=self.auth.refreshToken; 

    NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=refresh_token&refresh_token=%@&client_id=%@",clientSecret,strRefreshToken,clientID]; 
    NSLog(@"%@",post); 
    NSURL *url=[NSURL URLWithString:@"https://accounts.google.com/o/oauth2/token"]; 

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    NSURLConnection *con = [NSURLConnection connectionWithRequest:request delegate:self]; 
    [con start]; 
} 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
// The request is complete and data has been received 
// You can parse the stuff in your instance variable now 
NSString *responseJSON; 

// This flag indicates whether the response was received after an API call and out of the 
// following cases. 

// Convert the received data in NSString format. 
responseJSON = [[NSString alloc] initWithData:(NSData *)_receivedData encoding:NSUTF8StringEncoding]; 

NSDictionary *dictJsonNotification = 
[NSJSONSerialization JSONObjectWithData: [responseJSON dataUsingEncoding:NSUTF8StringEncoding] 
           options: NSJSONReadingMutableContainers 
            error: nil]; 
NSLog(@"dictJsonNotification %@",dictJsonNotification); 

[[NSUserDefaults standardUserDefaults] setObject:[dictJsonNotification objectForKey:@"access_token"] forKey:KDefaultGmailAccessToken]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

[self createCalendar1]; 

}

,然後使用查詢保存的訪問令牌

+0

一些解釋性文字去與答案的代碼:不能刷新通過向API呈現刷新令牌來訪問令牌。您需要通過在稱爲「刷新授權」的請求中將刷新令牌呈現給令牌端點「https:// accounts.google.com/o/oauth2/token」來獲取新的訪問令牌。 –

相關問題