如何在Android项目中使用WeDeploy API及OAuth跳转时获取Auth服务AccessToken
Hey there! Let's walk through your two WeDeploy questions for Android development clearly and practically:
Getting started with the WeDeploy API in your Android app is straightforward—here's the step-by-step breakdown:
Add the necessary dependencies
Open your app-levelbuild.gradlefile and include the WeDeploy Core dependency:dependencies { implementation 'com.wedeploy:wedeploy-core:2.0.0' }Sync your project to make sure the dependency installs correctly.
Initialize WeDeploy
It’s best to initialize WeDeploy once when your app starts. You can do this in a customApplicationclass (or your main Activity if you don’t have one):import com.wedeploy.android.WeDeploy; public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); // Replace with your actual WeDeploy project URL WeDeploy.initialize("your-project-url.wedeploy.io"); } }If you use a custom Application class, don’t forget to declare it in your
AndroidManifest.xml.Make API calls
Once initialized, you can interact with your WeDeploy services. For example, fetching data from a database service:WeDeploy.data("your-service-name") .get("/items") .execute(new Callback<JsonObject>() { @Override public void onSuccess(JsonObject response) { // Handle the retrieved data here } @Override public void onFailure(Exception e) { // Handle any errors that occur } }); }
When using WeDeploy Auth’s OAuth flow that redirects back to an Activity, here’s how you can get the access token:
Configure your callback Activity
First, set up your target Activity to receive the OAuth redirect inAndroidManifest.xml. Add an intent-filter to capture your redirect URL (replace placeholders with your actual values):<activity android:name=".OAuthCallbackActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Match your WeDeploy Auth redirect URI --> <data android:scheme="your-scheme" android:host="your-host" /> </intent-filter> </activity>Handle the redirect intent
In yourOAuthCallbackActivity, extract the authorization code from the incoming intent, then exchange it for an access token using the WeDeploy Auth SDK:import com.wedeploy.android.auth.WeDeployAuth; import com.wedeploy.android.auth.AuthException; public class OAuthCallbackActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handleOAuthRedirect(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleOAuthRedirect(intent); } private void handleOAuthRedirect(Intent intent) { Uri uri = intent.getData(); if (uri != null) { String code = uri.getQueryParameter("code"); if (code != null) { // Initialize Auth with your service URL WeDeployAuth auth = WeDeployAuth.init("your-auth-service-url.wedeploy.io"); // Exchange code for access token (match redirect URL to your WeDeploy settings) auth.exchange(code, "your-redirect-url") .execute(new Callback<Session>() { @Override public void onSuccess(Session session) { // Access token is available here String accessToken = session.getAccessToken(); // Use this token for authenticated API requests } @Override public void onFailure(Exception e) { // Handle token exchange errors if (e instanceof AuthException) { AuthException authException = (AuthException) e; // Handle specific authentication errors } } }); } } } }The
Sessionobject also provides refresh tokens and user details if you need them later. Just ensure the redirect URL used inexchange()matches exactly what’s configured in your WeDeploy Auth service.
内容的提问来源于stack exchange,提问作者albert

