如何刷新我从iOS中的谷歌oauth 2.0获得的令牌 [英] How to Refresh the token that i got from google oauth 2.0 in iOS

查看:147
本文介绍了如何刷新我从iOS中的谷歌oauth 2.0获得的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个iOS应用程序,它使用用户的谷歌帐户从他的YouTube帐户中获取数据并显示它们....
第一步是使用gtm2来验证用户并获取访问令牌和刷新令牌
的问题是访问令牌在60分钟后到期,我必须登录并再次允许应用程序...
i发现你可以使用刷新令牌来获取一个新的访问令牌
使用这个来自文档:
- >我的问题是如何发出POST请求以获取Objective-c
中的访问令牌这是我需要的数据使用:

I am making an iOS application that uses the user's google account to get data from his youtube account and show them .... first step is done using the gtm2 to authenticate the user and get an acces-token and a refresh-token the problem is that the access-token expires after 60 minutes and i have to login and allow the application again... i have found that you can use the refresh-token to get a new access-token using this from the documetation : --> my question is how to make a POST request to get the access token in objective-c this is the data i need to use:

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

这是我正在使用的代码:

this is the code i am using :

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);

我得到的错误是:
{
error:invalid_request
}

the error i get is : { "error" : "invalid_request" }

推荐答案

你错过了 = in您的格式字符串。

You are missing a = in your format string.

更改

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];

这篇关于如何刷新我从iOS中的谷歌oauth 2.0获得的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆