调用serviceAccount.json抛出FileNotFoundException

时间:2019-04-09 05:36:27

标签: java android filenotfoundexception

我正在尝试在我的项目中实现Google API,该项目需要调用传递凭据的serviceAccount.json。

这是我的代码:

    public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String SERVICE_ACCOUNT_CREDENTIAL_FILE = "/home/and03/AndroidStudioProjects/Parent/app/src/main/res/assets/serviceAccount.json";


    private static final String OAUTH_SCOPE = "https://www.googleapis.com/auth/androidmanagement";
    AndroidManagement androidManagement;
    String json,abcd;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        try {
            FileInputStream input = new FileInputStream(SERVICE_ACCOUNT_CREDENTIAL_FILE);
            GoogleCredential credential = GoogleCredential.fromStream(input).createScoped(Collections.singleton(OAUTH_SCOPE));
            androidManagement = new AndroidManagement(GoogleNetHttpTransport.newTrustedTransport(),JacksonFactory.getDefaultInstance(),credential);
            //createEnterprise(androidManagement);
            Log.e(TAG, "onCreate: "+ androidManagement );
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private String createEnterprise(AndroidManagement androidManagementClient)
            throws IOException {
        SignupUrl signupUrl =
                androidManagementClient
                        .signupUrls()
                        .create()
                        .setProjectId("managementtest")
                        .setCallbackUrl("https://unsplash.com/v1/signupUrls")
                        .execute();

        String enterpriseToken = displayUrlToAdmin(signupUrl.getUrl());

        Enterprise enterprise =
                androidManagementClient
                        .enterprises()
                        .create(new Enterprise())
                        .setProjectId("managementtest")
                        .setSignupUrlName(signupUrl.getName())
                        .setEnterpriseToken(enterpriseToken)
                        .execute();

        return enterprise.getName();
    }

    private String displayUrlToAdmin(String url) {
        Log.i("My Url",url);
       return url;
    }
}

这是我从此处描述的Google文档中的示例中获取的代码

Android Management API: create an enterprise

我知道我们需要FileInputStream从资产文件夹中调用此文件,但它仍显示filenotfoundException。

还有另一个示例应用程序,可以显示完整的演示

退房:

Sample App for Android Management API

我只想知道如何在我的应用程序中使用.json文件的路径,以便它可以正确运行。

0 个答案:

没有答案