英文:
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&playlistId=PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T&_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&part=snippet&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 (
"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))
}
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 := &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("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)
}
log.Println(Jsonify(items))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论