我正在通过我的应用制作 youtube搜索视频。为此,我在 google开发者控制台上创建了一个项目,并提供凭据,例如包名称和 SHA - 1证书获取在我的项目中使用的YouTubeApi key
,但是当我运行程序时,它会向GoogleJsonResponseException exception
发送一些消息。我不明白exception
是什么,我们如何解决它。
以下是例外
05-07 15:26:23.858 12602-12738/com.dp.videostoreadmin W/System.err: There was a service error: 403 : The Android package name and signing-certificate fingerprint, null and null, do not match the app restrictions configured on your API key. Please use the API Console to update your key restrictions.
下面是我的活动代码
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.SearchListResponse;
import com.google.api.services.youtube.model.SearchResult;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String YOUTUBE_API_KEY = "AIzaSyDielPMOnvIiOh5Rh3MyPRfFTprFmcX0Cw";
EditText searchText;
Button submit;
private static YouTube youtube;
private static final long NUMBER_OF_VIDEOS_RETURNED = 25;
String queryKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchText = (EditText) findViewById(R.id.editText);
submit = (Button) findViewById(R.id.submit);
queryKey = searchText.getText().toString();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new getSearchResult().execute();
}
});
}
public class getSearchResult extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
try {
youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("VideoStoreAdmin").build();
YouTube.Search.List search = youtube.search().list("id,snippet");
search.setKey(YOUTUBE_API_KEY);
search.setQ(queryKey);
search.setType("video");
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/publishedAt,snippet/thumbnails/default/url),nextPageToken");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
// Call the API and print results.
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
if (searchResultList != null) {
Log.d("TAG",searchResultList.toString());
}
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
}
}