使用刷新访问令牌提供"请求失败:未授权(401)"

时间:2014-12-24 11:23:19

标签: ios objective-c oauth google-api google-oauth

我在google calendar api工作。

以下是用于创建日历的代码

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)" (REFRESH TOKEN用作用户不需要每次都登录)

对于在用户第一次授权应用程序后检索的正常访问令牌(未过期),它可以正常工作。

以下是运行时url,它仅在未使用刷新的访问令牌之前有效。

实际网址:https://www.googleapis.com/calendar/v3/calendars?key=1080844328725-t8jo4s3cpqtg9orcp63lkujh1dvsqugq.apps.googleusercontent.com&access_token=1/x4c8cfLrfYamVgkBqSsi85N34wX7O_IxFutis08BGaN90RDknAdJa_sgfheVM0X

1 个答案:

答案 0 :(得分: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];

}

然后使用已保存的访问令牌进行查询