2017-03-31 66 views
0

我很努力使Google云数据流Java SDK能够与PubSub源协同工作。下面的简约代码总是带有401(未授权)错误。我试图找出如何提供凭据,但无法找到一种方法来使其工作。我之前使用过gcloud auth登录,以确保我已登录且订阅存在。Apache Beam Java SDK和PubSub源示例

以下错误被抛出:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli) on project my-dataflow: An exception occured while executing the Java class. null: InvocationTargetException: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized 
[ERROR] { 
[ERROR] "code" : 401, 
[ERROR] "errors" : [ { 
[ERROR] "domain" : "global", 
[ERROR] "message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", 
[ERROR] "reason" : "unauthorized" 
[ERROR] } ], 
[ERROR] "message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", 
[ERROR] "status" : "UNAUTHENTICATED" 
[ERROR] } 

我的代码如下:

public class MyDataflow{ 

    public static void main(String[] args) { 

    PipelineOptions options = PipelineOptionsFactory.create(); 

    PubsubOptions dataflowOptions = options.as(PubsubOptions.class); 
    dataflowOptions.setStreaming(true); 

    Pipeline p = Pipeline.create(options); 

    p.apply(PubsubIO.<String>read().subscription("projects/my-project/subscription/my-subscription") 
      .withCoder(StringUtf8Coder.of())); 

    // Run the pipeline. 
    p.run().waitUntilFinish(); 
    } 
} 
+1

您是否尝试过使用'gcloud身份验证应用程序默认login',而不是'gcloud权威性login'? –

+0

谢谢汗,你指出我正确的方向。我之前做过这件事,但事实证明,我的GOOGLE_APPLICATION_CREDENTIALS环境变量已设置为该文件夹,而不是直接设置为json凭证文件。如果您将其添加为答案,我可以接受它。 – jimmy

+0

您可以添加此作为答案? –

回答