0

我正在爲我的公司(在我的空閒時間)創建一個網絡/應用程序,爲我們的客戶提供訪問權限,以檢索並回復我們爲他們發佈的應用程序的Google評論。 Google已發佈Reply to Reviews API這應該爲此工作。回覆來評論API | Google Play開發者API

我一直無法弄清楚獲取訪問部分。

我正在嘗試使用服務帳戶。我創建了一個OAuth2服務帳號,授予訪問權限,下載了一個私鑰,並且按照提供的樣本here(如下)累了。

我知道在這個例子中它使用的是Plus服務。 我的問題是,我應該使用什麼服務?我假設加服務不是我想要的。

以這種方式生成證書是否獲得auth_token的正確方法(在回覆評論API文檔中)?

感謝您的幫助!

using System; 
using System.Security.Cryptography.X509Certificates; 

using Google.Apis.Auth.OAuth2; 
using Google.Apis.Plus.v1; 
using Google.Apis.Plus.v1.Data; 
using Google.Apis.Services; 

namespace Google.Apis.Samples.PlusServiceAccount 
{ 
/// <summary> 
/// This sample demonstrates the simplest use case for a Service Account service. 
/// The certificate needs to be downloaded from the Google Developers Console 
/// <see cref="https://console.developers.google.com/"> 
/// "Create another client ID..." -> "Service Account" -> Download the certificate, 
/// rename it as "key.p12" and add it to the project. Don't forget to change the Build action 
/// to "Content" and the Copy to Output Directory to "Copy if newer". 
/// </summary> 
public class Program 
{ 
    // A known public activity. 
    private static String ACTIVITY_ID = "z12gtjhq3qn2xxl2o224exwiqruvtda0i"; 

    public static void Main(string[] args) 
    { 
     Console.WriteLine("Plus API - Service Account"); 
     Console.WriteLine("=========================="); 

     String serviceAccountEmail = "SERVICE_ACCOUNT_EMAIL_HERE"; 

     var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable); 

     ServiceAccountCredential credential = new ServiceAccountCredential(
      new ServiceAccountCredential.Initializer(serviceAccountEmail) 
      { 
       Scopes = new[] { PlusService.Scope.PlusMe } 
      }.FromCertificate(certificate)); 

     // Create the service. 
     var service = new PlusService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = credential, 
      ApplicationName = "Plus API Sample", 
     }); 

     Activity activity = service.Activities.Get(ACTIVITY_ID).Execute(); 
     Console.WriteLine(" Activity: " + activity.Object.Content); 
     Console.WriteLine(" Video: " + activity.Object.Attachments[0].Url); 

     Console.WriteLine("Press any key to continue..."); 
     Console.ReadKey(); 
    } 
} 
} 

回答

0

我的問題是,我應該什麼樣的服務來使用?我假設加服務不是我想要的。

要使用回覆評論API,您需要使用OAuth客戶端或服務帳戶提供授權。

所以有兩種方法。

  1. 使用的OAuth2:安裝應用

編輯/resources/client_secrets.json文件,並添加客戶端ID, 客戶端密鑰和重定向URI。使用其 main()方法執行任何樣本類以開始認證流程:

瀏覽器窗口將打開並要求您登錄。確保帳戶 在Google Play Developer控制檯中擁有適當的權限。 接受權限對話框。瀏覽器應顯示 認證流程已完成。關閉窗口並返回到您的IDE的 並檢查控制檯輸出。該腳本將輸出apks的列表 。令牌將存儲在您的主文件夾中的.store/android_publisher_api 中。刪除此文件以重新啓動授權流程。

  • 使用的OAuth2:服務帳戶編輯ApplicationConfig.java並添加服務帳戶的電子郵件地址。將在Google API控制檯中生成的服務帳戶密鑰 複製到同一目錄中,並將 重命名爲key.p12。在您的IDE中使用其main()方法 執行任何樣本類該腳本將輸出一個apks列表。
  • 下面是Github一個完整的樣本。

    +0

    對不起,我應該提到我試圖在C#中做到這一點。你知道使用C#來完成這個任務嗎? –

    +0

    所以我有了使用Java的Github示例。在Reply to Reviews API文檔中,它說要檢索一組評論: 「使用GET方法爲您的應用程序請求評論列表。在您的請求中,爲您的應用程序添加完全限定的軟件包名稱,例如com.google.android.apps.maps-以及您在獲得API訪問權時獲得的授權令牌。「 GET GET https://www.googleapis.com/androidpublisher/v2/applications/your_package_name/reviews? access_token = your_auth_token' **什麼是授權令牌?** @noogui –