2016-06-08 37 views
2

我試圖從我的應用內訪問YouTube數據API v3,以從特定通道獲取一些視頻。我創建了一個API密鑰並插入了我的包名稱和SHA-1指紋。但是,它不起作用。YouTube數據API v3身份驗證錯誤

這是錯誤:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden 
{ 
    "code" : 403, 
    "errors" : [ { 
     "domain" : "usageLimits", 
     "message" : "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.", 
"reason" : "ipRefererBlocked", 
"extendedHelp" : "https://console.developers.google.com/apis/credentials?project=1097633804344" 
    } ], 
     "message" : "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." 
} 
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) 
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) 
at de.axelrindle.youtubeapitest.YoutubeConnector.fetchVideoIDS(YoutubeConnector.java:59) 
at de.axelrindle.youtubeapitest.util.VideoIDFetcher.fetchVideoIDS(VideoIDFetcher.java:62) 
at de.axelrindle.youtubeapitest.InitService.onHandleIntent(InitService.java:57) 
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.os.HandlerThread.run(HandlerThread.java:61) 

這裏是代碼,它發送請求:

YoutubeConnector.class 

public YoutubeConnector(Context context) { 
    this.context = context; 
    youTube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() { 
     @Override 
     public void initialize(HttpRequest request) throws IOException {} 
    }).setApplicationName("YouTube Data API v3 Test").build(); 

    try { 
     query = youTube.search().list("id,snippet"); 
     query.setKey(DevKey.YOUTUBE_API); 
     query.setType("video"); 
     query.setFields("items(id/videoId,snippet/title,snippet/description,snippet/thumbnails/default/url)"); 
     query.setChannelId("UC3ifTl5zKiCAhHIBQYcaTeg"); 
     query.setMaxResults(maxResults); 
    } catch (IOException e) { 
     Log.e("YoutubeConnector", "Failed to connect to YouTube: " + e.getMessage()); 
    } 
} 

public List<String> fetchVideoIDS() { 

    List<String> ids = new ArrayList<>(); 
    maxResults += 10; 

    try { 
     SearchListResponse response = query.execute(); 
     List<SearchResult> results = response.getItems(); 

     for (SearchResult result : results) { 
      ids.add(result.getId().getVideoId()); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return ids; 
} 

任何幫助是MUCH感謝!

+0

確保您正在運行的密鑰庫文件(構建apk)和相同的文件SHA1用於創建API密鑰。 – Bharatesh

+0

@skadoosh我用一個和相同的密鑰庫進行簽名和生成一個API密鑰。 – Axel

+0

請問我有一個問題,如何聲明查詢變量? – rainman

回答

1

我自己修復了。

我剛剛使用了錯誤的API密鑰。

編輯:

也許我應該提到的是,當我使用的瀏覽器API密鑰,而不是一個Android的API密鑰工作。