Home > OS >  API is not working while doing apk from tutorial. "errorcode":"steps.oauth.v2.FailedT
API is not working while doing apk from tutorial. "errorcode":"steps.oauth.v2.FailedT

Time:01-17

I am doing an application from this tutorial: https://youtu.be/xi94n-gwpWA Apk shows 5day weather for specific location.

I am using AccuWeather API (https://developer.accuweather.com/)

I've done all the same as it is on the video and when I compile the apk it is all ok, but when I click on link generated after I run my apk as it is on the vid (01:15, link above) it gives me an error:

{"fault":{"faultstring":"Failed to resolve API Key variable request.queryparam.apikey","detail":{"errorcode":"steps.oauth.v2.FailedToResolveAPIKey"}}}

It should be data in JSON generated by an API instead of that error.

This is my code:

package com.example.pogoda;

import android.net.Uri;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

public class NetworkUtils {
private static final String TAG = "NetworkUtils";
    private final static String WEATHERDB_BASE_URL=
        "http://dataservice.accuweather.com/forecasts/v1/daily/5day/1385520";

    private final static String API_KEY = "here is my api key";

    private final static String PARAM_API_KEY = "api_key";

    public static URL buildUrlForWeather(){
        Uri builtUri = Uri.parse(WEATHERDB_BASE_URL).buildUpon()
            .appendQueryParameter(PARAM_API_KEY, API_KEY)
            .build();

        URL url = null;
        try {
            url= new URL(builtUri.toString());
        }catch (MalformedURLException e)    {
            e.printStackTrace();
        }

         Log.i(TAG, "buildUrlForWeather: url: " url);
        return url;

}

public static String getResponseFromHttpUrl(URL url) throws IOException {
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
        InputStream in = urlConnection.getInputStream();

        Scanner scanner = new Scanner(in);
        scanner.useDelimiter("\\A");

        boolean hasInput = scanner.hasNext();
        if(hasInput) {
            return scanner.next();
        }else {
            return null;
        }
    } finally {
        urlConnection.disconnect();
    }
}

If something is not really clear for you guys, ask questions please. As it is my first post here I may made some mistakes.

CodePudding user response:

I think you have two problems here.

Here's the documentation for the api endpoint you're using. Notice that the API Key parameter is apikey not api_key.

The other problem is that you cannot use a random string as an api key. An API key purpose is for the api provider to be able to authorise, limit and eventually bill your api usage. You need to use the api key which was provided to you by accuweather upon creating an account with them. Here is their pricing and packages page.

You can read more on the topic here.

  •  Tags:  
  • Related