请求的授权令牌是什么? (YouTube API) [英] What is the authorization token for the request? (Youtube API)

查看:101
本文介绍了请求的授权令牌是什么? (YouTube API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用完整的C / Curl之前,我试图按照Google的Youtube API进行PHP / Curl的可恢复上传。这是我的代码。

 <?php 

$ resource =" www.googleapis.com/upload/ youtube / v3 / videos?uploadType = resumable& part =摘要,状态,contentDetails" ;;
$ query ='{
snippet:{
title:我的视频标题,
description:这是描述我的视频,
标签:[酷,视频,更多关键字],
categoryId:22
},
status:{
privacyStatus: public,
embeddable:是,
license: youtube
}
}’;

$ response = NULL;

#初始化PHP / CURL句柄
$ ch = curl_init();

$ request_headers = array();
$ request_headers [] =‘授权:承载*****’;
$ 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); //根据需要添加head
curl_setopt($ ch,CURLOPT_NOBODY,FALSE); //返回正文
curl_setopt($ ch,CURLOPT_URL,$ resource); //目标站点
curl_setopt($ ch,CURLOPT_VERBOSE,TRUE); //最小化日志
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE); //没有证书
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,TRUE); //跟随重定向
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE); //返回字符串

#创建返回数组
$ response = curl_exec($ ch);
$ info = curl_getinfo($ ch);
$ error = curl_error($ ch);

#关闭PHP / CURL句柄
curl_close($ ch);

返回$响应;

?>

每当我运行脚本时,都会收到403 Forbidden错误。我认为问题出在授权令牌上。我认为我没有使用应有的功能。 Google说


该请求设置了以下HTTP请求标头:



  • 授权–请求的授权令牌。


我不确定确切是什么,因此我使用了使用Youtube PHP Client API生成的访问令牌,但仍然没有。

解决方案

检查此代码。



在这里,您可以在响应头中获取location参数。请参阅下面的代码和我得到的响应头。另外,在 $ requestBody



<中使用干净的 JSON 数据pre> <?php

函数startResumableSession(){
$ requestBody ='{ snippet:{ title: test video , description: testing api, tags:[ any, thing], categoryId:25}, status:{ privacyStatus: public, embeddable: true ,许可证: youtube}}';
$ headers =数组

POST / upload / youtube / v3 / videos?uploadType = resumable& part = snippet,status HTTP / 1.1,
Host:www .googleapis.com,
内容长度:0,
内容类型:application / json,
授权:承载xxxxxxxxxxxxxACCESS_TOKENxxxxxxxxxxxxxxxxxx,
);
/ *请求URL中的参数值必须经过URL编码。 * /
$ url = https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,状态;
$ 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);
/ *调试信息* /
return array('response'=>(array)$ result,
'info'=> $ info,
'headers'= > $ headers
);
}

这是响应的标题。在这里,您可以找到带有上传ID的位置。对于下一步,请访问此链接保存可恢复会话URI

  HTTP / 1.1 200已建立连接
HTTP / 1.1 200 OK
X-GUploader-UploadID:AEnB2UqLAR0Gf5xGi9pDGEYgMkrajFAuaC33IpiY-2WM2hspQe30DxUz2qLEL
位置:https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status&upload_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxfxxyfxyyfjEfYeOYeOYeOjYeOYeOjDyEOYBYBYKYOYKYOYeOYJYOYJYJYKYJYYOYJYJYJYJYJYJYJJJJJJYJYJYJY这是我的朋友
变量:原始
变量:X-Origin
X-Goog-Correlation-Id:Z14VW6zFX9E
缓存控制:无缓存,无存储,最大年龄= 0 ,必须重新验证
语法:no-cache
过期:1990年1月1日,星期一00:00:00 GMT
日期:6月16日,星期五2017 08:36:22 GMT
内容长度:0
服务器:UploadServer
内容类型:text / html; charset = UTF-8
Alt-Svc:quic =:443; ma = 2592000; v = 38,37,36,35


I'm trying to follow Google's Youtube API on resumable upload in PHP/Curl before I go full C/Curl. Here is my code.

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

?>

Whenever I run the script I get a 403 Forbidden error. I think the issue comes from the authorization token. I think I'm not using what I am supposed to. Google says

"The request sets the following HTTP request headers:

  • Authorization – The authorization token for the request."

I am not sure what it is exactly, so I used an access token generated with Youtube PHP Client API but still nothing.

解决方案

check this code. Maybe this can help you.

In this, you can get the location parameter in the response header. Please see below code and response header that I got. Also, use clean and clear JSON data in $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
    );
}

This is the header of the response. In this, you can find the location with upload id. For next step please visit this link 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"

这篇关于请求的授权令牌是什么? (YouTube API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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