Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

google-app-engine - 如何使用Go on App Engine提取youtube播放列表视频(how to fetch youtube playlist videos using Go on app engine)

Using Api key I was able to fetch the videos in a playlist from Api Explorer .

(使用Api键,我可以从Api Explorer中获取播放列表中的视频。)

Execute without OAuth fetched the results json.

(没有OAuth的执行将获取结果json。)

Here is the link.

(链接在这里。)
https://developers.google.com/apis-explorer/?hl=en_US#p/youtube/v3/youtube.playlistItems.list?part=snippet&playlistId=PLHyTuYqPkZCzt7mWZ4hmmrRdjLJiw6O2T&_h=2&

(https://developers.google.com/apis-explorer/?hl=zh_CN#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:

(使用Go on App引擎实施相同的调用失败,并显示以下错误:)

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/

Here is the code I use:

(这是我使用的代码:)

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 ie goapp serve

(到目前为止,我在本地开发服务器上运行我的代码,即goapp serve)

What is missing?

(缺什么?)

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

(如何使用v3 api和ApiKey提取youtube播放列表视频?)

  ask by suman j translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

(在App Engine上,您需要使用urlfetch包提供的特殊http.Transport ;)

see https://cloud.google.com/appengine/docs/go/urlfetch/

(请参阅https://cloud.google.com/appengine/docs/go/urlfetch/)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...