2012-06-06 56 views
3

我知道这里有很多类似的问题,但我尝试了几个小时的这些解决方案,但他们没有为我工作。我总是得到一个{ "error" : "unauthorized_client" }"。我想以编程方式刷新我的accesstoken以使用Youtube API。我已经获得了一个清晰的说法。来自PHP的Google身份验证

这是从来就想出:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'client_secret' => '<mysecret>', 
'grant_type' => 'refresh_token', 
'refresh_token' => '<my_refresh_token>', 
'client_id' => '<my_client_id>.apps.googleusercontent.com', 
'redirect_url'=>'<my_redirect_uri>' 
)); 
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
echo var_dump(curl_exec($ch)); 

哪些错误呢?

+0

PS:获取一个新的令牌在这里工作得很好:https://code.google.com/oauthplayground/ –

+0

没有想法?我真的不知道它有什么问题...... –

回答

2

您指出的URL和查询参数对我来说很合适。似乎如果您提供的用于生成新令牌的client_id与为获取refresh_token而提供的client_id不同,则会出现此错误。

可能发生的一件事是,如果您使用Google的OAuth操场生成了access_token和refresh_token,然后尝试使用该refresh_token生成新的令牌 - 这将无效。 Google OAuth操场正在使用不同的client_id来发出请求,这肯定会导致您记录的“unauthorized_client”错误。

Temboo为Google提供了一个非常简洁且易于使用的OAuth库。你可以在这里查看:https://www.temboo.com/library/Library/Google/OAuth/

(全面披露:我在Temboo工作)