从Delphi访问TRAKT API-承载身份验证问题[已解决] [英] Accessing TRAKT API from Delphi - issues with Bearer authentication [SOLVED]
问题描述
通过使用TOauth2Authenticator,TRESTClient,TRESTRequest,TRESTResponseDataSet,TRESTResponse,TFDmemtable和TDataSource,我设法连接到Trakt API,并对我的应用程序进行身份验证并获得代码作为回报,最后使用该代码来获取令牌.
Using TOauth2Authenticator, TRESTClient, TRESTRequest, TRESTResponseDataSet, TRESTResponse, TFDmemtable and a TDataSource have I managed to connect to the Trakt API, and authenticate my application and get a code in return and lastly use that code to get a token.
所以现在我应该可以使用以下代码列出电影了.
So now I should be able to list the movies with the below code.
RESTClient1.BaseURL := 'https://trakt.tv/';
RESTRequest1.Resource := 'calendars/my/movies/{start_date}/{days}';
RESTRequest1.AddParameter('Authorization','Bearer ' + TraktAccessToken.Text,pkHTTPHEADER);
RESTRequest1.AddParameter('trakt-api-version', '2', pkHTTPHEADER);
RESTRequest1.AddParameter('trakt-api-key', TraktClientId.Text, pkHTTPHEADER);
RESTRequest1.Params.AddItem('start_date', '2019-05-05', pkURLSEGMENT); // Dummy date
RESTRequest1.Params.AddItem('days', '7', pkURLSEGMENT); // Dummy time
RESTRequest1.Method := TRESTRequestMethod.rmGET;
RESTRequest1.Execute;
但这是行不通的.该命令将授权设置为授权=承载[令牌]".而我在尝试阅读后的理解是,它必须是我"Authorization:Bearer [TOKEN]".(注意"="与:")
But this wouldn't work. The command sets the Authorization to "Authorization=Bearer [TOKEN]" whereas my understanding after trying to read up is that it must me "Authorization:Bearer [TOKEN]". (Mind the "=" vs ":")
我尝试删除授权"行,并用戳戳而不是标题中的数据代替组件属性,如下所示:
I have tried removing the Authorization line and replacing with poking the data not in the headers but the component properties, like so:
Trakt_OAuth.TokenType := TOAuth2TokenType.ttBEARER;
Trakt_OAuth.AccessToken := TraktAccessToken.Text;
也是这样:
RESTRequest1.AddAuthParameter('Authorization','Bearer ' + TraktAccessToken.Text, TRESTRequestParameterKind.pkHTTPHEADER);
两个选项都以完全相同的方式失败.当您在客户标题中戳字符串时,我唯一找到的就是此选项:
Both options fail in the very same manner. The only thing I have located is this option, when you poke a string into your customer headers:
Request.CustomHeaders.Add('Authorization: Bearer ' + sToken)
在RESTRequest类中,CustomHeaders方法不可用.
By the CustomHeaders method isn't available in the RESTRequest class.
考虑到我准备好认真修复它,我意识到我可以读出参数项:
Thinking I was ready to go really dirty to fix it, I realised I could read out the parameter item:
Whatever := RESTRequest1.Params.Items[5].ToString
现在我可以更改字符串-替换为"="带有:",但这无济于事,因为我无法将其写回.
Now I can change the string - replace the "=" with a ":", but that doesn't help as I can't write it back.
这不是我遇到了一个极端的情况-OAuth2提供一个令牌,然后将其与承载身份验证结合使用是这样的.我只是似乎做不到...
It's not that I have run into a corner case - OAuth2 giving a token that is then used in conjunction with bearer authentication is how this works. I just seems to fail to do it ...
解决方案:我的API调用现在可以使用了.我没有在相应的HTTP标头属性中手动添加任何Authorize,而是设置了OAuth2属性,然后该组件似乎负责为我构建标头.
Solution: My API calls now work. I don't manually add any the Authorize in the respective HTTP Header property, but set the OAuth2 properties, and then the component seemingly takes care of building the headers for me.
Trakt_OAuth.TokenType := TOAuth2TokenType.ttBEARER;
Trakt_OAuth.AccessToken := TraktAccessToken.Text;
推荐答案
这对我有用:
LParam := RESTRequest1.Params.AddItem;
LParam.Name := 'Authorization';
LParam.Kind := TRESTRequestParameterKind.pkHTTPHEADER;
LParam.Value := 'Bearer ' + sToken;
我似乎想起一项服务,在Bearer和令牌之间必须没有空格字符.很奇怪.
I seem to recall for one service, there needed to be no space character between Bearer and the token. Weird.
这篇关于从Delphi访问TRAKT API-承载身份验证问题[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!