如何在Go语言的App Engine上获取YouTube播放列表视频

huangapple go评论73阅读模式
英文:

how to fetch youtube playlist videos using Go on app engine

问题

使用 API 密钥,我能够从 Api Explorer 中获取播放列表中的视频。不使用 OAuth 执行可以获取结果的 JSON。以下是链接:
https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.playlistItems.list?part=snippet&playlistId=PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T&_h=2&

在 App Engine 上使用 Go 实现相同的调用会失败,并显示以下错误:

Get https://www.googleapis.com/youtube/v3/playlistItems?alt=json&part=snippet&playlistId=PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T: http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/

这是我使用的代码:

import (
	"net/http"
	"code.google.com/p/google-api-go-client/googleapi/transport"
	"code.google.com/p/google-api-go-client/youtube/v3"
	"log"
)

var service *youtube.Service
func init() {
	var err error
	log.Println("Apikey = ", apiKey)
	client := &http.Client{Transport: &transport.APIKey{Key: apiKey}}
	service, err = youtube.New(client)
	if err != nil {
		log.Println("ERROR in creating youtube New client ", err)
	}
	var items *youtube.PlaylistItemListResponse
	if items, err = service.PlaylistItems.List("snippet").PlaylistId("PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T").Do(); err != nil {
		log.Println("Error in fetching playlist items ", err) //this line shows the error
	}
	log.Println(Jsonify(items))
}

目前,我在本地开发服务器上运行代码,即 goapp serve

有什么遗漏的吗?如何使用 v3 API 和 API 密钥获取 YouTube 播放列表视频?

英文:

Using Api key I was able to fetch the videos in a playlist from Api Explorer. Execute without OAuth fetched the results json. Here is the link.<br>
https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.playlistItems.list?part=snippet&amp;playlistId=PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T&amp;_h=2&

Implementing the same call using Go on App engine fails with below error:
<pre>
Get https://www.googleapis.com/youtube/v3/playlistItems?alt=json&amp;part=snippet&amp;playlistId=PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T: http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/
</pre>

Here is the code I use:
<!-- language : go -->

import (
	&quot;net/http&quot;
	&quot;code.google.com/p/google-api-go-client/googleapi/transport&quot;
	&quot;code.google.com/p/google-api-go-client/youtube/v3&quot;
	&quot;log&quot;
)

    var service *youtube.Service
    func init() {
    	var err error
    	log.Println(&quot;Apikey = &quot;, apiKey)
    	client := &amp;http.Client{Transport: &amp;transport.APIKey{Key: apiKey}}
    	service, err = youtube.New(client)
    	if err != nil {
    		log.Println(&quot;ERROR in creating youtube New client &quot;, err)
    	}
    	var items *youtube.PlaylistItemListResponse
    	if items, err = service.PlaylistItems.List(&quot;snippet&quot;).PlaylistId(&quot;PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T&quot;).Do(); err != nil {
    		log.Println(&quot;Error in fetching playlist items &quot;, err) //this line shows the error
    	}
    	log.Println(Jsonify(items))
    }

As of now, I run my code on local dev server i.e goapp serve

What is missing? How do I fetch youtube playlist videos using v3 api and ApiKey?

答案1

得分: 0

很抱歉,链接的文档并没有完全解释为什么你的代码不起作用。在App Engine上,你需要使用urlfetch包提供的特殊http.Transport;请参考https://cloud.google.com/appengine/docs/go/urlfetch/。

英文:

Unfortunately, the linked doc doesn't quite explain why your code isn't working. On App Engine, you need to use a special http.Transport provided by the urlfetch package; see https://cloud.google.com/appengine/docs/go/urlfetch/

答案2

得分: 0

找到了解决方案。下面的代码完成了我的任务。

func FetchVideos(w http.ResponseWriter, r *http.Request) {
    var service *youtube.Service
    ctx := appengine.NewContext(r)
    
    transport := &transport.APIKey{
        Key:       apiKey,
        Transport: &urlfetch.Transport{Context: ctx}}
    client := &http.Client{Transport: transport}
    
    var err error
    service, err = youtube.New(client)
    if err != nil {
        log.Println("创建 youtube New client 时出错", err)
    }
    var items *youtube.PlaylistItemListResponse
    if items, err = service.PlaylistItems.List("snippet").PlaylistId("PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T").Do(); err != nil {
        log.Println("获取播放列表项时出错", err)
    }
    log.Println(Jsonify(items))
}

请注意,这只是代码的翻译部分,不包括任何其他内容。

英文:

Found the solution. Below code did the task for me.

<!-- language: go -->

func FetchVideos(w http.ResponseWriter, r *http.Request) {
    var service *youtube.Service
    ctx := appengine.NewContext(r)
    
    transport := &amp;transport.APIKey{
    	Key:       apiKey,
    	Transport: &amp;urlfetch.Transport{Context: ctx}}
    client := &amp;http.Client{Transport: transport}
    
    var err error
    service, err = youtube.New(client)
    if err != nil {
    	log.Println(&quot;ERROR in creating youtube New client &quot;, err)
    }
    var items *youtube.PlaylistItemListResponse
    if items, err = service.PlaylistItems.List(&quot;snippet&quot;).PlaylistId(&quot;PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T&quot;).Do(); err != nil {
    	log.Println(&quot;Error in fetching playlist items &quot;, err)
    }
    log.Println(Jsonify(items))

huangapple
  • 本文由 发表于 2016年1月3日 14:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/34573979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定