【问题标题】:Accessing Ubuntu One API results in "401 UNAUTHORIZED" or "400 BAD REQUEST"访问 Ubuntu One API 会导致“401 UNAUTHORIZED”或“400 BAD REQUEST”
【发布时间】:2012-10-27 09:16:22
【问题描述】:

我正在使用带有 Indy 10 的 Delphi XE2 来访问 Ubuntu One API。到目前为止,一切都很好。我能够以described 从云端获取OAuth 令牌。现在我想开始使用 API(比如described):

function TUbuntuOneApi.DoRequest(const URL: String): String;
var
  RequestParams: TStringList;
  HeaderIndex: Integer;
const
  OAuthAuthorisationHeader = 'OAuth realm="", oauth_version="%s", oauth_nonce="%s", oauth_timestamp="%s", oauth_consumer_key="%s", oauth_token="%s", oauth_signature_method="%s", oauth_signature="%s"';

begin
  RequestParams := SignOAuthRequestParams(URL, fOAuthAppToken, fOAuthAccessToken);

{  OAuth realm="", oauth_version="1.0",
  oauth_nonce="$nonce", oauth_timestamp="$timestamp",
  oauth_consumer_key="$consumer_key", oauth_token="$token",
  oauth_signature_method="PLAINTEXT",
  oauth_signature="$consumer_secret%26$token_secret"
  }

  HeaderIndex := IdHTTP.Request.CustomHeaders.IndexOfName('Authorization');
  if HeaderIndex >= 0 then
  begin
    IdHTTP.Request.CustomHeaders.Delete(HeaderIndex);
  end;

  // Solving: http://stackoverflow.com/questions/7323036/twitter-could-not-authenticate-with-oauth-401-error
  IdHTTP.Request.CustomHeaders.FoldLines := false;

  IdHTTP.Request.CustomHeaders.AddValue('Authorization', Format(OAuthAuthorisationHeader, [
      TIdURI.URLDecode(RequestParams.Values['oauth_version']),
      TIdURI.URLDecode(RequestParams.Values['oauth_nonce']),
      TIdURI.URLDecode(RequestParams.Values['oauth_timestamp']),
      TIdURI.URLDecode(RequestParams.Values['oauth_consumer_key']),
      TIdURI.URLDecode(RequestParams.Values['oauth_token']),
      TIdURI.URLDecode(RequestParams.Values['oauth_signature_method']),
      TIdURI.URLDecode(RequestParams.Values['oauth_signature'])
    ]));

  // Execute
  Result := IdHTTP.Get(URL);
  RequestParams.Free;
end;

function TUbuntuOneApi.Test: String;
begin
  //Result := DoRequest('https://one.ubuntu.com/api/file_storage/v1');
  Result := DoRequest('https://one.ubuntu.com/api/account/');
end;

调用 https://one.ubuntu.com/api/file_storage/v1 会导致“401 UNAUTHORIZED”。调用 https://one.ubuntu.com/api/account/ 会导致“400 BAD REQUEST”。 OAuth 库来自 SourceForge 并与 Dropbox 一起使用(如果 OAuth-Params 在查询字符串中,Dropbox 接受它们)。令牌是正确且有效的。我做错了什么?

顺便说一句:

function SignOAuthRequestParams(const URL: String; OAuthAppToken, OAuthAccessToken: TOAuthToken): TStringList;
var
  Consumer: TOAuthConsumer;
  ARequest: TOAuthRequest;
  Response: String;
  HMAC: TOAuthSignatureMethod;
begin
  HMAC := TOAuthSignatureMethod_PLAINTEXT.Create;
  // Consumer erzeugen
  Consumer := TOAuthConsumer.Create(OAuthAppToken.Key, OAuthAppToken.Secret);

  // Request erzeugen
  ARequest := TOAuthRequest.Create(URL);
  ARequest := ARequest.FromConsumerAndToken(Consumer, OAuthAccessToken, URL);
  ARequest.Sign_Request(HMAC, Consumer, OAuthAccessToken);

  Result := TStringList.Create;
  Result.AddStrings(ARequest.Parameters);

  ARequest.Free;
  Consumer.Free;
  HMAC.Free;
end;

【问题讨论】:

    标签: delphi oauth indy10


    【解决方案1】:

    此时,新创建的令牌可用于验证 其他方法,在 Ubuntu SSO 服务上,但还不能与 Ubuntu One API。我们需要告诉 Ubuntu One 复制新的 从 SSO 服务创建的令牌。这是通过发出GET 来完成的 对以下 URL 的请求,使用新的 OAuth 令牌签名。 来自Ubuntu One: Authorization page

    真可惜,我忘了这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 2018-09-21
      • 2016-03-01
      • 1970-01-01
      相关资源
      最近更新 更多