2012-12-23 43 views
7

Tôi đang tạo một ứng dụng iOS sử dụng tài khoản google của người dùng để lấy dữ liệu từ tài khoản youtube của mình và hiển thị chúng ... bước đầu tiên được thực hiện bằng cách sử dụng gtm2 để xác thực người dùng và nhận mã thông báo và mã thông báo làm mới vấn đề là mã thông báo truy cập hết hạn sau 60 phút và tôi phải đăng nhập và cho phép ứng dụng lại ... tôi thấy rằng bạn có thể sử dụng mã thông báo làm mới để nhận mã thông báo truy cập mới sử dụng mã này từ tài liệu: -> câu hỏi của tôi là cách thực hiện yêu cầu POST để nhận mã thông báo truy cập trong mục tiêu-c đây là dữ liệu tôi cần sử dụng:Cách làm mới mã thông báo mà tôi nhận được từ google oauth 2.0 trong iOS

POST /o/oauth2/token HTTP/1.1 

Host: accounts.google.com 
Content-Type: application/x-www-form-urlencoded 

client_id=21302922996.apps.googleusercontent.com& 
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB& 
refresh_token=1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ& 
grant_type=refresh_token 

này là mã tôi đang sử dụng:

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

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

    NSString *postLength = [NSString stringWithFormat:@"%d", [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]; 


    NSError *error; 
    NSURLResponse *response; 
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",data); 

lỗi tôi nhận được là: { "lỗi": "invalid_request" }

+0

Tôi không quen thuộc với các thư viện client Objective-C, nhưng hầu hết các thư viện client các ngôn ngữ khác có hỗ trợ cho tự động làm mới mã thông báo OAuth 2 cho bạn khi bạn thực hiện yêu cầu bằng mã thông báo truy cập đã hết hạn. Bạn có chắc chắn rằng bạn cần phải làm mới theo cách thủ công không? Nếu vậy, nơi tốt nhất để hỏi có lẽ là https://groups.google.com/forum/?fromgroups#!forum/gdata-objectivec-client –

Trả lời

2

Bạn đang thiếu một = trong định dạng của bạn chuỗi.

Thay đổi

NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=refresh_token&refresh_token=%@&client_id%@",kGoogleClientSecretKey,kRefreshToken,kGoogleClientIDKey]; 

để

NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=refresh_token&refresh_token=%@&client_id=%@",kGoogleClientSecretKey,kRefreshToken,kGoogleClientIDKey]; 
+1

Cảm ơn bạn rất nhiều + Gabriele bạn là người tiết kiệm cuộc sống :) – Rifinio

+1

vui mừng Cứu giúp. Hãy xem xét chấp nhận câu trả lời nếu nó giải quyết được vấn đề của bạn;) –

+0

Xong .......... – Rifinio

Các vấn đề liên quan