2016-09-06 13 views
0

我已經使用完整的youtube api代碼插入commentthread youtube_v3,但是當我調用api時,它給了我下面的錯誤。

Authorization Required 

You need to authorize access before proceeding. 

當我點擊授權訪問其redired到谷歌的錯誤頁面,並顯示以下內容

400. That’s an error. 

Error: redirect_uri_mismatch 

Application: Application Name 

You can email the developer of this application at: email address 

The redirect URI in the request, http://localhost/done.php, does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/******-*******.apps.googleusercontent.com?project=******* to update the authorized redirect URIs. 

在這段代碼中,$_GET['code']$_SESSION['token']未設置,使我無法獲得訪問令牌等等它給我redirect_uri_mismatch錯誤。

從哪裏設置值$_GET['code']$_SESSION['token']。 代碼:

if (isset($_GET['code'])) { 
    if (strval($_SESSION['state']) !== strval($_GET['state'])) { 
    die('The session state did not match.'); 
    } 

    $client->authenticate($_GET['code']); 
    $_SESSION['token'] = $client->getAccessToken(); 
    header('Location: ' . $redirect); 
} 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

給我的解決方案,如果你有先進的

感謝。

+0

你爲什麼認爲錯誤與這段代碼有關?錯誤消息非常清楚。 – jeroen

+0

是@jeroen,但是從$ _GET ['code']和$ _SESSION ['token']的值被設置? 我一行一行地寫出完美的代碼。 –

回答

0

根據此thread,返回響應的重定向URI必須在API控制檯中註冊,並且error表示您沒有這樣做,或者沒有正確完成。

轉到您的項目的控制檯,並在API訪問下查找。您應該在那裏看到您的客戶端ID &以及重定向URI列表。如果您想要的URI未列出,請單擊編輯設置並將URI添加到列表中。

您還可以查看這些相關的SO問題:

希望這有助於!

相關問題