2011-07-02 45 views
3

我嘗試連接到谷歌的API,它返回: failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized 我請求訪問令牌和它返回我的XXX,在那之後我用下面的PHP代碼連接到谷歌API:請求用戶的電子郵件返回未經授權的401錯誤

$opts = array('http' => 
     array(
     'method' => 'GET', 
     'header' => "Authorization: OAuth $access_token\r\nContent-Type: application/json\r\n", 
     ) 
    ); 
    $context = stream_context_create($opts); 
    $result = file_get_contents('https://www.googleapis.com/userinfo/email?alt=json',false,$context); 

問題在哪裏寫錯了標題或其他的東西? 更新:$ access_token是我通過請求獲取的值。

+0

'$ access_token'中有什麼? – hakre

+0

[完整的OAuth示例](http://tools.ietf.org/html/rfc5849#section-1.2),[PHP Google API Oauth示例](http://stackoverflow.com/questions/2084087/oauth-with- PHP換谷歌-API) – hakre

回答

0

檢查如何做到這一點在PHP,而是會在登錄客戶端,以谷歌的時候,你必須設置RequestPermission參數,這樣你可以從用戶的配置文件獲取信息

requestPermissions:'https://www.googleapis.com/auth/userinfo.profile'

相關問題