从 Delphi 访问 TRAKT API - 不记名身份验证问题 [已解决] [英] Accessing TRAKT API from Delphi - issues with Bearer authentication [SOLVED]

查看:18
本文介绍了从 Delphi 访问 TRAKT API - 不记名身份验证问题 [已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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]".而我在尝试阅读后的理解是它必须是我授权:不记名[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)

CustomHeaders 方法在 RESTRequest 类中不可用.

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 Header 属性中手动添加任何 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆