使用URL编码的LinkedIn API调用

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

LinkedIn API call with url encoded

问题

例如,如果我发起一个调用:

https://api.linkedin.com/v1/people/~:(id,first-name,last-name)

它应该正常工作,但如果URL进行编码(应该这样做)如下:

https://api.linkedin.com/v1/people/~:%28id,first-name,last-name%29

我会收到以下错误消息:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
    <status>404</status>
    <timestamp>1423577265744</timestamp>
    <request-id>6B2UQSA25W</request-id>
    <error-code>0</error-code>
    <message>[invalid.property.name]. Couldn't find property with name {:%28id,first-name,last-name%29} in resource of type {Person}</message>
</error>

你如何处理这个问题?
目前我无法更改代码,因为我的语言(顺便说一下,是Golang)没有很好地使用标准包进行编码。这是我的代码:

r, _ := http.NewRequest("GET", "https://api.linkedin.com/v1/people/~:(id,first-name,last-name)", nil)
r.Header.Set("Authorization", "Bearer "+respBody.AccessToken)
resp, err = http.DefaultClient.Do(r)

除了重写标准库代码,你有没有解决这个问题的方法?

自2013年以来,一些人面临了相同的问题,可以在这里找到更多信息:https://developer.linkedin.com/forum/edit-i-dont-way-uris-look

英文:

For example, if I make a call to:

https://api.linkedin.com/v1/people/~:(id,first-name,last-name)

It should work fine, but if the URL encoded (and it should be) like this:

https://api.linkedin.com/v1/people/~:%28id,first-name,last-name%29

I get this error message:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
    <status>404</status>
    <timestamp>1423577265744</timestamp>
    <request-id>6B2UQSA25W</request-id>
    <error-code>0</error-code>
    <message>[invalid.property.name]. Couldn't find property with name {:%28id,first-name,last-name%29} in resource of type {Person}</message>
</error>

How can you deal with that?
Currently I cannot change the code because my language (Golang btw) do very properly encode this with standar package. Here is my code:

r, _ := http.NewRequest("GET", "https://api.linkedin.com/v1/people/~:(id,first-name,last-name)", nil)
r.Header.Set("Authorization", "Bearer "+respBody.AccessToken)
resp, err = http.DefaultClient.Do(r)

Do you have solution for this issue except rewrite std lib code?

Some what face with the same issue since 2013 here https://developer.linkedin.com/forum/edit-i-dont-way-uris-look

答案1

得分: 1

将请求创建后,将URL的Opaque字段设置为路径:

r, err := http.NewRequest("GET", "https://api.linkedin.com", nil)
if err != nil {
  // 处理错误
}
r.URL.Opaque = "/v1/people/~:(id,first-name,last-name)"
r.Header.Set("Authorization", "Bearer "+respBody.AccessToken)
resp, err = http.DefaultClient.Do(r)

URL类型的文档描述了如何进行此操作。

英文:

Set the URL Opaque field to the path after creating the request:

r, err := http.NewRequest("GET", "https://api.linkedin.com", nil)
if err != nil {
  // handle error
}
r.URL.Opaque = "/v1/people/~:(id,first-name,last-name)"
r.Header.Set("Authorization", "Bearer "+respBody.AccessToken)
resp, err = http.DefaultClient.Do(r)

The URL type documentation describes how to do this.

huangapple
  • 本文由 发表于 2015年2月10日 22:12:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/28433779.html
匿名

发表评论

匿名网友

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

确定