Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
Add API Key Parameter to OpenWeatherMap API Call
Browse files Browse the repository at this point in the history
  • Loading branch information
cdlei committed Oct 16, 2015
1 parent 6f18226 commit 4bfbe6f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Expand Up @@ -17,6 +17,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildTypes.each {
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
}
}

dependencies {
Expand Down
Expand Up @@ -27,6 +27,7 @@
import android.text.format.Time;
import android.util.Log;

import com.example.android.sunshine.app.BuildConfig;
import com.example.android.sunshine.app.MainActivity;
import com.example.android.sunshine.app.R;
import com.example.android.sunshine.app.Utility;
Expand Down Expand Up @@ -98,12 +99,14 @@ public void onPerformSync(Account account, Bundle extras, String authority, Cont
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
final String APPID_PARAM = "APPID";

Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, locationQuery)
.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(UNITS_PARAM, units)
.appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
.appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
.build();

URL url = new URL(builtUri.toString());
Expand Down

19 comments on commit 4bfbe6f

@Star55555
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do we add the key in gradle.properties

@Star55555
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and could not find property MyOpenWeatherMapApiKey

@cdlei
Copy link
Contributor Author

@cdlei cdlei commented on 4bfbe6f Oct 28, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Star55555 please following the steps in the repository's README.md file underneath the section "Api Key Is Required"

Here's a link to it.
https://github.com/udacity/Sunshine-Version-2/blob/sunshine_master/README.md

Hope that helps.

@smithThapa
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this course is really frustating........it always says gradle sync failed when i add the key

@Sinapse87
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smithThapa do you think it is a course fault if your IDE is trying to sync?

@EslamAbdo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"We recommend that each student obtain a key via the following instructions, and include the unique key for the build by adding the following line to [USER_HOME]/.gradle/gradle.properties

MyOpenWeatherMapApiKey="<UNIQUE_API_KEY"> "

this is NOT working

@jReadthis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', ' " xxxxxxxxxxxxxxxxxx " '

replace x's with key

@Anand-M-P
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nano2583 Yes that solved the problem. Thanks for the solution.

Follow these steps if you have any confusion.

  1. http://home.openweathermap.org/ and create an account if you don't have one already. Once you successfully create an account you will be provided with a unique API key.
  2. Look for this line in your build.gradle file
    -> it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
    and replace it with
    -> it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY, ' " xxxxxxxxxxxxxxxxxx " '

@sindhu238
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we find that sync folder??
I dont find it in the app folder...

@fady-zarif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where should i paste this code please
import android.text.format.Time;
import android.util.Log;

+import com.example.android.sunshine.app.BuildConfig;
import com.example.android.sunshine.app.MainActivity;
import com.example.android.sunshine.app.R;
import com.example.android.sunshine.app.Utility;
@@ -98,12 +99,14 @@ public void onPerformSync(Account account, Bundle extras, String authority, Cont
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";

  •        final String APPID_PARAM = "APPID";
    
          Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
                  .appendQueryParameter(QUERY_PARAM, locationQuery)
                  .appendQueryParameter(FORMAT_PARAM, format)
                  .appendQueryParameter(UNITS_PARAM, units)
                  .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
    
  •                .appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
                  .build();
    
          URL url = new URL(builtUri.toString());
    

@RujulS
Copy link

@RujulS RujulS commented on 4bfbe6f Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Anand-M-P Yes it works. Thank you

@markmaclaren
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did SunshineSyncAdapter.java come from? I don't even have that file.

@Shubhamsharda
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where do i put this key. I cant find gradle.properties

@sandarumk
Copy link

@sandarumk sandarumk commented on 4bfbe6f Jun 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shubhamsharda You put the key in app/build.gradle file as given here in the first file. In your IDE the file will be under Gradle Scripts. Gradle scripts has two files called build.gradle. One is for the project - build.gradle (Project:Sunshine). The other one is for the app - build.gradle (Module:app) You put the key instead of 'MyOpenWeatherMapApiKey'. For example if your key is 123abc, it will go like
buildTypes.each {
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', ' "123abc" '
}

@HeatherKoyuk
Copy link

@HeatherKoyuk HeatherKoyuk commented on 4bfbe6f Jun 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You all are missing the point. Your in-app gradle files will be included in any code checked in - where in fact you should have your API key completely hidden and never checked in.

Let's say my USER_HOME is this: "C:\Users\Heather". In this USER_HOME is my AndroidStudioProject folder, containing the current project folder which contains some gradle files (C:\Users\Heather\AndroidStudioProjects\Sunshine-Version-2.gradle, or [USER_HOME]\AndroidStudioProjects[CURRENT_PROJECT].gradle for other projects). However, the point is to NOT have the private API key stored anywhere in there. Instead, under C:\Users\Heather, or whatever your USER_HOME is, there should be a .gradle folder. Open that folder. If this folder does not already contain a gradle.properties file, create a gradle.properties file or copy from [USER_HOME]\AndroidStudioProjects[CURRENT_PROJECT].gradle\gradle.properties. (If it does already contain a gradle.properties file just use that one.) Open the gradle.properties file directly under [USER_HOME].gradle\ and add:

MyOpenWeatherMapApiKey="xxxxx"

-where xxxxx is your API key.

Note that you will have to turn on hidden files probably to get to [USER_HOME].gradle - and you will have to use the file system instead of the IDE to get there. And that that folder may be missing the gradle.properties file - if so, just create one by coping over or creating a new file called gradle.properties as stated above.

If you do this with every private API key you have and just add the variable name in the in-app gradle files, your API keys will remain secure and you no longer have to worry about checking them in by mistake.

Hope this makes sense.

@Suleman-Tanveer
Copy link

@Suleman-Tanveer Suleman-Tanveer commented on 4bfbe6f Sep 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sandarumk thanks for your help. It made my day.

@ekamdz
Copy link

@ekamdz ekamdz commented on 4bfbe6f Oct 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HELP i am new...

In order to make changes for the weather API key i made some change in gradle.build and now my project fails to sync gradle. Earlier project was fine running on my android device too.

Here's the main screen

capture

PLEASE HELP ME IN MAKING CHANGES(whatever required) MANUALLY FOR CONTINUING MAKING SUNSHINE WEATHER APP WITH REAL WEATHER DATA.

I AM STUCK AT LESSON 2 OF DEVELOPING ANDROID APP WHERE THE ISSUE OF "KEY" ARISES.

THANKS

@meryrua
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekamdz I hope you've already solved your problem. If not gradle.properties is looked for in GRADLE_USER_HOME - for me it's "/home/[user]/.gradle", not "home/[user]/[AndroidStudioProjectFolder]/.gradle". I spent about hour to try understand why this API key could not be found :)

@ApoorvaTiwari
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can anyone please explain me the complete sunshine app ?

Please sign in to comment.