亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何轉換圖像以上傳 Spotify 個人資料圖片?

如何轉換圖像以上傳 Spotify 個人資料圖片?

Go
慕神8447489 2023-05-22 16:06:45
我正在設置一個圖像編碼器功能,您可以在其中輸入一個圖像 URL,它會返回它的 ISO-8859-1 版本。我將如何編寫一個向 URL 發送 HTTP GET 請求并將這些字節轉換為 ISO-8859-1 的函數?下面的代碼是我目前所擁有的一切。func grabImageBytes(imageURL string) ([]byte, error) {    req, _ := http.NewRequest("GET", imageURL, nil)    res, _ := http.DefaultClient.Do(req)    defer res.Body.Close()    body, err := ioutil.ReadAll(res.Body)    if err != nil {        return nil, err    } else {        return body, nil    }}其他功能:func getRandomImage(keyword string) (string, error) {    req, _ := http.NewRequest("GET", "https://www.google.com/search?tbm=isch&q="+keyword, nil)    req.Header.Add("authority", "www.google.com")    req.Header.Add("upgrade-insecure-requests", "1")    req.Header.Add("referer", "https://images.google.com/")    req.Header.Add("accept-language", "en-US,en;q=0.9")    res, _ := http.DefaultClient.Do(req)    defer res.Body.Close()    body, _ := ioutil.ReadAll(res.Body)    var imageURL string    if strings.Contains(string(body), ",\"ou\":\"") {        imageURL = strings.Split(strings.Split(string(body), ",\"ou\":\"")[1], "\",\"ow\":")[0]    } else {        return "", errors.New("Image not found.")    }    req2, _ := http.NewRequest("GET", imageURL, nil)    res2, _ := http.DefaultClient.Do(req2)    defer res2.Body.Close()    if res2.StatusCode == 404 {        return "", errors.New("Image not found.")    } else {        return imageURL, nil    }}
查看完整描述

1 回答

?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

您聲稱 Spotify 個人資料圖片是ISO 8850-1編碼/加密的說法毫無意義。

更有意義的是它是 Base64 編碼的。

例如,

面向開發人員的 Spotify:Web API:上傳自定義播放列表封面圖片。

Base64 編碼的 JPEG 圖像數據,最大負載大小為 256 KB


在圍棋中,

包base64

import "encoding/base64"

base64 包實現了 RFC 4648 指定的 base64 編碼。


另一個證據:“UTF-8 格式的 HTTPS 請求”

面向開發人員的 Spotify:Web API

要求

Spotify Web API 基于 REST 原則。通過以 UTF-8 格式向 API 端點發送標準 HTTPS 請求來訪問數據資源。


例如。使用您的 Stack Overflow 個人資料圖片:

package main


import (

? ? "encoding/base64"

? ? "fmt"

? ? "io/ioutil"

? ? "net/http"

? ? "os"

)


func grabImageBytes(imageURL string) ([]byte, error) {

? ? req, err := http.NewRequest("GET", imageURL, nil)

? ? if err != nil {

? ? ? ? return nil, err

? ? }

? ? res, err := http.DefaultClient.Do(req)

? ? if err != nil {

? ? ? ? return nil, err

? ? }

? ? defer res.Body.Close()

? ? body, err := ioutil.ReadAll(res.Body)

? ? if err != nil {

? ? ? ? return nil, err

? ? }

? ? enc := base64.StdEncoding

? ? img := make([]byte, enc.EncodedLen(len(body)))

? ? enc.Encode(img, body)

? ? return img, nil

}


func main() {

? ? imageURL := `https://lh5.googleusercontent.com/-P8ICR-LXoBs/AAAAAAAAAAI/AAAAAAAAE04/fVAeB6_nMeg/photo.jpg?sz=328`

? ? img, err := grabImageBytes(imageURL)

? ? if err != nil {

? ? ? ? fmt.Fprintln(os.Stderr, err)

? ? ? ? return

? ? }

? ? fmt.Println(string(img))

}


查看完整回答
反對 回復 2023-05-22
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號