2017-06-09 49 views
1

我試着在完成C/Curl之前,在PHP/Curl中關注Google的Youtube API。這是我的代碼。什麼是請求的授權令牌? 「Youtube API」

<?php 

    $resource   = "www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails"; 
    $query    = '{ 
     "snippet": { 
     "title": "My video title", 
     "description": "This is a description of my video", 
     "tags": ["cool", "video", "more keywords"], 
     "categoryId": 22 
     }, 
     "status": { 
     "privacyStatus": "public", 
     "embeddable": True, 
     "license": "youtube" 
     } 
    }'; 

    $response   = NULL; 

    # Initialize PHP/CURL handle 
    $ch     = curl_init(); 

    $request_headers = array(); 
    $request_headers[] = 'Authorization: Bearer *****'; 
    $request_headers[] = 'Content-Length: 278'; 
    $request_headers[] = 'Content-Type: application/json; charset=UTF-8'; 
    $request_headers[] = 'X-Upload-Content-Length: 3000000'; 
    $request_headers[] = 'X-Upload-Content-Type: video/*'; 

    curl_setopt($ch, CURLOPT_HTTPHEADER  , $request_headers   ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS  , $query     ); 
    curl_setopt($ch, CURLOPT_POST   , TRUE      ); 
    curl_setopt($ch, CURLOPT_HTTPGET  , FALSE     ); 
    curl_setopt($ch, CURLOPT_HEADER   , FALSE     );  // Include head as needed 
    curl_setopt($ch, CURLOPT_NOBODY   , FALSE     );  // Return body 
    curl_setopt($ch, CURLOPT_URL   , $resource    );  // Target site 
    curl_setopt($ch, CURLOPT_VERBOSE  , TRUE      );  // Minimize logs 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE     );  // No certificate 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION , TRUE      );  // Follow redirects 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE      );  // Return in string 

    # Create return array 
    $response   = curl_exec($ch); 
    $info    = curl_getinfo($ch); 
    $error    = curl_error($ch); 

    # Close PHP/CURL handle 
    curl_close($ch); 

    return $response; 

?> 

每當我運行腳本,我得到一個403 Forbidden錯誤。我認爲問題來自授權令牌。我想我沒有用我應該做的。谷歌表示,

「的要求設置下列HTTP請求頭:

  • 授權 - 授權令牌的請求。」

我不知道它到底是什麼,所以我用的Youtube PHP客戶端API,但仍然沒有產生一個訪問令牌。

+0

看來你剛纔發佈的API認證證書的公共論壇,你會想改變/立即撤銷他們。 –

+0

@AlexHowansky謝謝,已經改變!我以爲這隻會持續一個小時 –

+0

你和403一起得到什麼? –

回答

2

檢查此代碼。也許這可以幫助你。

在此,您可以在響應頭中獲取位置參數。請參閱下面的代碼和響應頭文件。此外,使用乾淨清晰JSON數據在$requestBody

<?php 

function startResumableSession() { 
    $requestBody = '{"snippet":{"title":"test video","description":"testing api","tags":["any","thing"],"categoryId":25},"status":{"privacyStatus":"public","embeddable":"true","license":"youtube"}}'; 
    $headers = array 
    (
     "POST /upload/youtube/v3/videos?uploadType=resumable&part=snippet,status HTTP/1.1", 
     "Host: www.googleapis.com", 
     "Content-length: 0", 
     "Content-type: application/json", 
     "Authorization: Bearer xxxxxxxxxxxxxACCESS_TOKENxxxxxxxxxxxxxxxxxx", 
    ); 
    /* Parameter values in the request URL must be URL-encoded. */ 
    $url = "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status"; 
    $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,urlencode($requestBody)); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_PROXY, "192.168.10.5:8080"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE); 
    $result = curl_exec($ch); 
    $info = curl_getinfo($ch); 
    $json = json_decode($result); 
    /* debug info */ 
    return array('response'=>(array)$result, 
     'info' => $info, 
     'headers'=>$headers 
    ); 
} 

這是響應的報頭中。在這裏,你可以找到帶有上傳ID的位置。對於下一步請訪問此鏈接Save the resumable session URI

HTTP/1.1 200 Connection established 
    HTTP/1.1 200 OK 
    X-GUploader-UploadID: AEnB2UqLAR0Gf5xGi9pDGEYgMkrajFAuaC33IpiY-2WM2hspQe30DxUz2qzELRenLWOyrK8x9S3He51SXGmHU6olQzUGqGxbcw 
    Location: https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status&upload_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
    ETag: "m2yskBQFythfE4irbTIeOgYYfBU/V3uOxfY0pSsHgtj2XMjOGKOAp3o" 
    Vary: Origin 
    Vary: X-Origin 
    X-Goog-Correlation-Id: Z14VW6zFX9E 
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
    Pragma: no-cache 
    Expires: Mon, 01 Jan 1990 00:00:00 GMT 
    Date: Fri, 16 Jun 2017 08:36:22 GMT 
    Content-Length: 0 
    Server: UploadServer 
    Content-Type: text/html; charset=UTF-8 
    Alt-Svc: quic=":443"; ma=2592000; v="38,37,36,35" 
+0

我現在收到400錯誤代碼的消息是「解析錯誤」。讓它工作,你會得到的要點。 –

+0

如果我在'/ *檢查請求正文contenttype * /'之後刪除了所有內容我收到401錯誤無效憑證。我在哪裏獲得授權令牌? –

+0

@PaikuHan您可以從https://developers.google.com/youtube/registering_an_application –

1

https://www.googleapis.com/youtube/ ...現在需要的(https://開頭不僅僅是WWW)

也從YouTube API的網站

在API列表中,確保狀態爲ON YouTube數據API V3。

如果您的應用程序將使用任何需要用戶授權的API方法,請閱讀身份驗證指南以瞭解如何實施OAuth 2.0授權。

+0

添加https://將錯誤更改爲400錯誤的請求 –