如何oAuth天气雅虎api

时间:2016-04-29 10:00:45

标签: delphi delphi-7 indy weather-api

我使用此代码访问来自雅虎的天气数据,一切正常。 不知怎的,这停止了从雅虎那里得到一个“糟糕的请求”......

“请提供有效的凭据.OAuth oauth_problem =”OST_OAUTH_PARAMETER_ABSENT_ERROR“,realm =”yahooapis.com“ “

我试着理解发生了什么,我认为这与雅虎的oAuth有关,但我不知道如何使用它和来自雅虎的文档糟透了......

以下代码..

mForcastTown.Add(MainForm.ExtraFE_IdHttp.Get('http://weather.yahooapis.com/forecastrss?w='+ mAdd_Town[mTonwNum].mWoeID +'&u='+ mAdd_Town[mTonwNum].mDegree);

谢谢...

... UPDATE

我在下面找到了这个,当在浏览器中运行时,我得到了我需要的xml,但当我运行它时

IdHttp.get('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid=55903793%20and%20u=%27c%27&format=xml') 

我发现了未知版本......

这是什么...... 谢谢......

1 个答案:

答案 0 :(得分:0)

我在Java上也遇到了同样的问题,也许这会将您引向某些领域。

在此link中,有一个示例Java代码:

    // Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Base64;
    import java.util.Base64.Encoder;
    import java.util.Random;
    import java.util.Collections;
    import java.net.URLEncoder;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.net.http.HttpResponse.BodyHandlers;
    import java.net.URI;

    /**
    *
    * <pre>
    * % java --version
    * % java 11.0.1 2018-10-16 LTS
    *
    * % javac WeatherYdnJava.java && java -ea WeatherYdnJava
    * </pre>
    *
    */
    public class WeatherYdnJava {
        public static void main(String[] args) throws Exception {

            final String appId = "test-app-id";
            final String consumerKey = "your-consumer-key";
            final String consumerSecret = "your-consumer-secret";
            final String url = "https://weather-ydn-yql.media.yahoo.com/forecastrss";

            long timestamp = new Date().getTime() / 1000;
            byte[] nonce = new byte[32];
            Random rand = new Random();
            rand.nextBytes(nonce);
            String oauthNonce = new String(nonce).replaceAll("\\W", "");

            List<String> parameters = new ArrayList<>();
            parameters.add("oauth_consumer_key=" + consumerKey);
            parameters.add("oauth_nonce=" + oauthNonce);
            parameters.add("oauth_signature_method=HMAC-SHA1");
            parameters.add("oauth_timestamp=" + timestamp);
            parameters.add("oauth_version=1.0");
            // Make sure value is encoded
            parameters.add("location=" + URLEncoder.encode("sunnyvale,ca", "UTF-8"));
            parameters.add("format=json");
            Collections.sort(parameters);

            StringBuffer parametersList = new StringBuffer();
            for (int i = 0; i < parameters.size(); i++) {
                parametersList.append(((i > 0) ? "&" : "") + parameters.get(i));
            }

            String signatureString = "GET&" +
                URLEncoder.encode(url, "UTF-8") + "&" +
                URLEncoder.encode(parametersList.toString(), "UTF-8");

            String signature = null;
            try {
                SecretKeySpec signingKey = new SecretKeySpec((consumerSecret + "&").getBytes(), "HmacSHA1");
                Mac mac = Mac.getInstance("HmacSHA1");
                mac.init(signingKey);
                byte[] rawHMAC = mac.doFinal(signatureString.getBytes());
                Encoder encoder = Base64.getEncoder();
                signature = encoder.encodeToString(rawHMAC);
            } catch (Exception e) {
                System.err.println("Unable to append signature");
                System.exit(0);
            }

            String authorizationLine = "OAuth " +
                "oauth_consumer_key=\"" + consumerKey + "\", " +
                "oauth_nonce=\"" + oauthNonce + "\", " +
                "oauth_timestamp=\"" + timestamp + "\", " +
                "oauth_signature_method=\"HMAC-SHA1\", " +
                "oauth_signature=\"" + signature + "\", " +
                "oauth_version=\"1.0\"";

            HttpClient client = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url + "?location=sunnyvale,ca&format=json"))
                .header("Authorization", authorizationLine)
                .header("X-Yahoo-App-Id", appId)
                .header("Content-Type", "application/json")
                .build();

            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
            System.out.println(response.body());
        }
    }

此代码的问题是oauthNonce是用随机字节生成的。在这里,错误是由无法识别的字符引起的。因为对于任何随机字节,由于系统会将其转换为字符串,因此可能存在系统无法识别和处理的任何字符。

我用这个替换整个部分:

String oauthNonce = RandomStringUtils.random(10, true, true);

它就像一种魅力。我目前没有任何错误,并且能够得到答复。希望对您有所帮助。