简体   繁体   English

Powershell Invoke-WebRequest和字符编码

[英]Powershell Invoke-WebRequest and character encoding

I am trying to get information from the Spotify database through their Web API. 我试图通过他们的Web API从Spotify数据库中获取信息。 However, I'm facing issues with accented vowels (ä,ö,ü etc.) 但是,我遇到重音元音(ä,ö,ü等)的问题

Lets take Tiësto as an example. 让我们以蒂斯托为例。 Spotify's API Browser can display the information correctly: https://developer.spotify.com/web-api/console/get-artist/?id=2o5jDhtHVPhrJdv3cEQ99Z Spotify的API浏览器可以正确显示信息: https : //developer.spotify.com/web-api/console/get-artist/?id=2o5jDhtHVPhrJdv3cEQ99Z

If I make a API call with Invoke-Webrequest I get 如果我使用Invoke-Webrequest进行API调用, Invoke-Webrequest得到

Ti??sto TI?STO

as name: 作为名称:

function Get-Artist {
param($ArtistID = '2o5jDhtHVPhrJdv3cEQ99Z',
      $AccessToken = 'MyAccessToken')


$URI = "https://api.spotify.com/v1/artists/{0}" -f $ArtistID

$JSON = Invoke-WebRequest -Uri $URI -Headers @{"Authorization"= ('Bearer  ' + $AccessToken)} 
$JSON = $JSON | ConvertFrom-Json
return $JSON
}

在此处输入图片说明

How can I get the correct name? 如何获得正确的名字?

Jeroen Mostert , in a comment on the question, explains the problem well: Jeroen Mostert在对该问题的评论中很好地解释了这个问题:

The problem is that Spotify is (unwisely) not returning the encoding it's using in its headers. 问题在于,Spotify(不明智地)没有返回其标头中使用的编码。 PowerShell obeys the standard by assuming ISO-8859-1 , but unfortunately the site is using UTF-8 . PowerShell通过假定ISO-8859-1遵守该标准 ,但不幸的是该站点使用的是UTF-8 (PowerShell ought to ignore standards here and assume UTF-8, but that's just like, my opinion, man.) More details here , along with the follow-up ticket. (PowerShell应该在这里忽略标准并采用UTF-8,但这就像我的看法一样。) 这里有更多详细信息,以及后续票据。

A workaround that doesn't require the use of temporary files is to re-encode the incorrectly read string . 不需要使用临时文件的解决方法重新编码不正确读取的string

If we assume the presence of a function convertFrom-MisinterpretedUtf8 ,we can use the following: 如果我们假设存在一个功能convertFrom-MisinterpretedUtf8 ,则可以使用以下代码:

$JSON = convertFrom-MisinterpretedUtf8 (Invoke-WebRequest -Uri $URI ...)

See below for the function's definition. 有关功能的定义,请参见下文。


Utility function convertFrom-MisinterpretedUtf8 : 实用程序功能convertFrom-MisinterpretedUtf8

function convertFrom-MisinterpretedUtf8([string] $String) {
  [System.Text.Encoding]::UTF8.GetString(
     [System.Text.Encoding]::GetEncoding(28591).GetBytes($String)
  )
}

The function converts the incorrectly read string back to bytes based on the mistakenly applied encoding (ISO-8859-1) and then recreates the string based on the actual encoding (UTF-8). 该函数根据错误应用的编码(ISO-8859-1)将错误读取的字符串转换回字节,然后根据实际编码(UTF-8)重新创建字符串。

Issue solved with the workaround provided by Jeron Mostert. Jeron Mostert提供的解决方法解决了问题。 You have to save it in a file and explicit tell Powershell which Encoding it should use. 您必须将其保存在文件中,并明确告诉Powershell它应使用哪种编码。 This workaround works for me because my program can take whatever time it needs (regarding read/write IO) 这种解决方法对我有用,因为我的程序可能需要任何时间(就读/写IO而言)

function Invoke-SpotifyAPICall {
param($URI,
      $Header = $null,
      $Body = $null
      )

if($Header -eq $null) {
    Invoke-WebRequest -Uri $URI -Body $Body -OutFile ".\SpotifyAPICallResult.txt"    
} elseif($Body -eq $null) {
    Invoke-WebRequest -Uri $URI -Headers $Header -OutFile ".\SpotifyAPICallResult.txt"
}

$JSON = Get-Content ".\SpotifyAPICallResult.txt" -Encoding UTF8 -Raw | ConvertFrom-JSON
Remove-Item ".\SpotifyAPICallResult.txt" -Force
return $JSON

}

function Get-Artist {
    param($ArtistID = '2o5jDhtHVPhrJdv3cEQ99Z',
          $AccessToken = 'MyAccessToken')


    $URI = "https://api.spotify.com/v1/artists/{0}" -f $ArtistID

    return (Invoke-SpotifyAPICall -URI $URI -Header @{"Authorization"= ('Bearer  ' + $AccessToken)})
}


Get-Artist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell Invoke-WebRequest XML 数据 - Powershell Invoke-WebRequest XML data 将 cURL 转换为 PowerShell Invoke-WebRequest - JSON 数据表问题 - Convert cURL to PowerShell Invoke-WebRequest - JSON data table issue Powershell Invoke-Webrequest w/ JSON Body - 无法反序列化……? - Powershell Invoke-Webrequest w/ JSON Body - Can not deserialize…? 将简单的curl数据请求转换为Powershell invoke-webrequest - convert simple curl data request to powershell invoke-webrequest 如何从 PowerShell 中的 Invoke-WebRequest 解析 JSON? - How to parse JSON from the Invoke-WebRequest in PowerShell? 如何解析/访问 Powershell 4 中 Invoke-WebRequest 返回的 JSON - How to parse/access JSON returned by Invoke-WebRequest in Powershell 4 Powershell:使用 Invoke-WebRequest,如何在 JSON 中使用变量? - Powershell: Using Invoke-WebRequest, How To Use Variable Within JSON? 使用PowerShell通过API查询Shopify JSON数据(Invoke-Webrequest错误401) - Querying Shopify JSON data via the API with PowerShell (Error 401 with Invoke-Webrequest) 如何为具有多个来自CSV变量的API调用设计“ Invoke-WebRequest”? (电源外壳) - How to design an “Invoke-WebRequest” for multiple API calls with more than one variable from CSV? (Powershell) 我如何显示在System.Object [] Powershell内部,invoke-webrequest - How can I show inside of System.Object[] Powershell, invoke-webrequest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM