Tôi đang sử dụng Delphi XE2 với Indy 10 để truy cập API Ubuntu One. Càng xa càng tốt. Tôi đã có thể nhận Mã thông báo OAuth từ Đám mây dưới dạng described. Bây giờ tôi muốn bắt đầu sử dụng API (như described):Truy cập vào các kết quả của Ubuntu One API trong "401 UNAUTHORIZED" hoặc "400 BAD REQUEST"
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;
Calling https://one.ubuntu.com/api/file_storage/v1 kết quả trong "401 TRÁI PHÉP". Gọi số https://one.ubuntu.com/api/account/ sẽ dẫn đến "400 BAD REQUEST". Thư viện OAuth là từ SourceForge và hoạt động với Dropbox (Dropbox chấp nhận OAuth-Params nếu chúng nằm trong Query-String). Các Tokens là chính xác và hợp lệ. Tôi đang làm gì sai?
BTW:
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;