This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API Key Parameter to OpenWeatherMap API Call
- Loading branch information
Showing
2 changed files
with
6 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4bfbe6f
There was a problem hiding this comment.
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
4bfbe6f
There was a problem hiding this comment.
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
4bfbe6f
There was a problem hiding this comment.
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.
4bfbe6f
There was a problem hiding this comment.
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
4bfbe6f
There was a problem hiding this comment.
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?
4bfbe6f
There was a problem hiding this comment.
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
4bfbe6f
There was a problem hiding this comment.
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
4bfbe6f
There was a problem hiding this comment.
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.
-> it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
and replace it with
-> it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY, ' " xxxxxxxxxxxxxxxxxx " '
4bfbe6f
There was a problem hiding this comment.
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...
4bfbe6f
There was a problem hiding this comment.
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";
4bfbe6f
There was a problem hiding this comment.
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
4bfbe6f
There was a problem hiding this comment.
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.
4bfbe6f
There was a problem hiding this comment.
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
4bfbe6f
There was a problem hiding this comment.
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" '
}
4bfbe6f
There was a problem hiding this comment.
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.
4bfbe6f
There was a problem hiding this comment.
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.
4bfbe6f
There was a problem hiding this comment.
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
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
4bfbe6f
There was a problem hiding this comment.
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 :)
4bfbe6f
There was a problem hiding this comment.
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 ?